These are common Git commands used in various situations:
start a working area (see also: git help tutorial) clone Clone a repository into a new directory init Create an empty Git repository or reinitialize an existing one
work on the current change (see also: git help everyday) add Add file contents to the index mv Move or rename a file, a directory, or a symlink restore Restore working tree files rm Remove files from the working tree and from the index sparse-checkout Initialize and modify the sparse-checkout
examine the history and state (see also: git help revisions) bisect Use binary search to find the commit that introduced a bug diff Show changes between commits, commit and working tree, etc grep Print lines matching a pattern log Show commit logs show Show various types of objects status Show the working tree status
grow, mark and tweak your common history branch List, create, or delete branches commit Record changes to the repository merge Join two or more development histories together rebase Reapply commits on top of another base tip reset Reset current HEAD to the specified state switch Switch branches tag Create, list, delete or verify a tag object signed with GPG
collaborate (see also: git help workflows) fetch Download objects and refs from another repository pull Fetch from and integrate with another repository or a local branch push Update remote refs along with associated objects
'git help -a' and 'git help -g' list available subcommands and some concept guides. See 'git help <command>' or 'git help <concept>' to read about a specific subcommand or concept. See 'git help git'for an overview of the system.
通过
git help <verb>、git <verb> --help、man git-<verb>
三种命令格式,可以只查看到指定命令的帮助信息,执行之后会通过默认的 Web
浏览器打开 Git 安装目录 mingw64/share/doc/git-doc 下的 HTML
帮助页面。例如,要获得 git config
命令的帮助信息,只需要执行 git help config 命令即可。
如果无需阅读详细的 HTML
帮助手册,而只需在命令行简单的展示使用参考,则可以采用 -h
选项来获得较为简明的帮助信息:
Config file location --global use global config file --system use system config file --local use repository config file --worktree use per-worktree config file -f, --file <file> use given config file --blob <blob-id> read config from given blob object
Action --get get value: name [value-pattern] --get-all get all values: key [value-pattern] --get-regexp get values for regexp: name-regex [value-pattern] --get-urlmatch get value specific for the URL: section[.var] URL --replace-all replace all matching variables: name value [value-pattern] --add add a new variable: name value --unset remove a variable: name [value-pattern] --unset-all remove all matches: name [value-pattern] --rename-section rename section: old-name new-name --remove-section remove a section: name -l, --list list all --fixed-value use string equality when comparing values to 'value-pattern' -e, --edit open an editor --get-color find the color configured: slot [default] --get-colorbool find the color setting: slot [stdout-is-tty]
Type -t, --type <> value is given this type --bool value is "true" or "false" --int value is decimal number --bool-or-int value is --bool or --int --bool-or-str value is --bool or string --path value is a path (file or directory name) --expiry-date value is an expiry date
Other -z, --null terminate values with NUL byte --name-only show variable names only --includes respect include directives on lookup --show-origin show origin of config (file, standard input, blob, command line) --show-scope show scope of config (worktree, local, global, system, command) --default <value> with --get, use default value when missing entry
hint: Using 'master' as the name for the initial branch. This default branch name hint: is subject to change. To configure the initial branch name to use in all hint: of your new repositories, which will suppress this warning, call: hint: hint: git config --global init.defaultBranch <name> hint: hint: Names commonly chosen instead of 'master' are 'main', 'trunk' and hint: 'development'. The just-created branch can be renamed via this command: hint: hint: git branch -m <name> Initialized empty Git repository in D:/Workspace/test/.git/
也只可以直接采用 git init 工程名称
格式的命令,初始化一个新的工程目录:
1 2 3
λ git init test
Initialized empty Git repository in D:/Workspace/test/.git/
使用 git status
命令查看哪些文件处于什么状态,克隆一个新仓库之后立即使用该命令,可以查看到类似如下输出:
1 2 3 4 5 6
λ git status
On branch master Your branch is up to date with 'origin/master'.
nothing to commit, working tree clean
由于 git status 命令的输出十分详细,可以采用
-s 或者 --short 选项简化状态信息的输出:
1 2 3
λ git status -s
λ git status --short
跟踪新文件
在工程下新建一个 README.md 文件,然后执行
git status 查看工程状态:
1 2 3 4 5 6 7 8 9 10 11
λ git status
On branch master
No commits yet
Untracked files: (use "git add <file>..." to include in what will be committed) README.md
nothing added to commit but untracked files present (use "git add" to track)
运行 git add 命令可以将 README.md
放置到暂存区,然后再运行 git status 命令查看工程状态:
1 2 3 4 5 6 7 8 9 10
λ git add README.md
λ git status On branch master
No commits yet
Changes to be committed: (use "git rm --cached <file>..." to unstage) new file: README.md
接下来,对 README.md 文件进行修改之后再保存,再一次运行
git status 查看工程状态:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
λ git status
On branch master
No commits yet
Changes to be committed: (use "git rm --cached <file>..." to unstage) new file: README.md
Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git restore <file>..." to discard changes in working directory) modified: README.md