Git 底层命令
目录
- git cat-file
- git ls-files
- git ls-tree
- git read-tree
- git write-tree
- git commit-tree
- git mktree
- git diff-tree
- git gc
- git verify-pack
- git hash-object
- git show-index
- git show-ref
- git update-ref
- git rev-parse
- git rev-list
- git for-each-ref
- git var
- git diff-tree
- git merge-base
- git ref-log
- git blame
- git check-attr
- git count-objects
- git fsck
git 的子命令
git 的子命令目前有 140+ 个,可以通过 git --exec-path
获取到 git 的子命令存储目录,如下显示:
1 | $ ls `git --exec-path` |
git cat-file
查看objects文件。
命令说明(点击展开)
usage: git cat-file (-t [--allow-unknown-type] | -s [--allow-unknown-type] | -e | -p | <type> | --textconv | --filters) [--path=<path>] <object>
or: git cat-file (--batch | --batch-check) [--follow-symlinks] [--textconv | --filters]
<type> can be one of: blob, tree, commit, tag
-t show object type
-s show object size
-e exit with zero when there's no error
-p pretty-print object's content
--textconv for blob objects, run textconv on object's content
--filters for blob objects, run filters on object's content
--path <blob> use a specific path for --textconv/--filters
--allow-unknown-type allow -s and -t to work with broken/corrupt objects
--buffer buffer --batch output
--batch[=<format>] show info and content of objects fed from the standard input
--batch-check[=<format>]
show info about objects fed from the standard input
--follow-symlinks follow in-tree symlinks (used with --batch or --batch-check)
--batch-all-objects show all objects with --batch or --batch-check
示例
1 | # 查看 objects 文件类型 |
git ls-files
查看index文件内容。
命令说明(点击展开)
usage: git ls-files [<options>] [<file>...]
-z paths are separated with NUL character
-t identify the file status with tags
-v use lowercase letters for 'assume unchanged' files
-f use lowercase letters for 'fsmonitor clean' files
-c, --cached show cached files in the output (default)
-d, --deleted show deleted files in the output
-m, --modified show modified files in the output
-o, --others show other files in the output
-i, --ignored show ignored files in the output
-s, --stage show staged contents' object name in the output
-k, --killed show files on the filesystem that need to be removed
--directory show 'other' directories' names only
--eol show line endings of files
--empty-directory don't show empty directories
-u, --unmerged show unmerged files in the output
--resolve-undo show resolve-undo information
-x, --exclude <pattern>
skip files matching pattern
-X, --exclude-from <file>
exclude patterns are read from <file>
--exclude-per-directory <file>
read additional per-directory exclude patterns in <file>
--exclude-standard add the standard git exclusions
--full-name make the output relative to the project top directory
--recurse-submodules recurse through submodules
--error-unmatch if any <file> is not in the index, treat this as an error
--with-tree <tree-ish>
pretend that paths removed since <tree-ish> are still present
--abbrev[=<n>] use <n> digits to display SHA-1s
--debug show debugging data
示例
1 | $ git ls-files |
git ls-tree
查看树内容,可以是 commit-id ,也可以是 tree-id ,也可以是 git revisions 格式。
命令说明(点击展开)
usage: git ls-tree [<options>] <tree-ish> [<path>...]
-d only show trees
-r recurse into subtrees
-t show trees when recursing
-z terminate entries with NUL byte
-l, --long include object size
--name-only list only filenames
--name-status list only filenames
--full-name use full path names
--full-tree list entire tree; not just current directory (implies --full-name)
--abbrev[=<n>] use <n> digits to display SHA-1s
1 | $ git ls-tree bace6b8b2d3058e3cc0495f5edfb235ed8cff21e |
git read-tree
把tree的信息(可以是多个)写入到索引(index)中。
命令说明(点击展开)
usage: git read-tree [(-m [--trivial] [--aggressive] | --reset | --prefix=<prefix>) [-u [--exclude-per-directory=<gitignore>] | -i]] [--no-sparse-checkout] [--index-output=<file>] (--empty | <tree-ish1> [<tree-ish2> [<tree-ish3>]])
--index-output <file>
write resulting index to <file>
--empty only empty the index
-v, --verbose be verbose
Merging
-m perform a merge in addition to a read
--trivial 3-way merge if no file level merging required
--aggressive 3-way merge in presence of adds and removes
--reset same as -m, but discard unmerged entries
--prefix <subdirectory>/
read the tree into the index under <subdirectory>/
-u update working tree with merge result
--exclude-per-directory <gitignore>
allow explicitly ignored files to be overwritten
-i don't check the working tree after merging
-n, --dry-run don't update the index or the work tree
--no-sparse-checkout skip applying sparse checkout filter
--debug-unpack debug unpack-trees
--recurse-submodules[=<checkout>]
control recursive updating of submodules
示例
1 | $ git read-tree 7fc6c35263716bad0d1df5a814766e8f1fd20345 |
git write-tree
把存储区的文件结构存储成 tree 对象。
命令说明(点击展开)
usage: git write-tree [–missing-ok] [–prefix=
--missing-ok allow missing objects
--prefix <prefix>/ write tree object for a subdirectory <prefix>
1 | # 待补充 |
git commit-tree
把 tree 对象读取到暂存区。
命令说明(点击展开)
usage: git commit-tree [(-p
1 | # 待补充 |
git mktree
根据输入(ls-tree
的输出格式)来生成 tree 对象。
usage: git mktree [-z] [–missing] [–batch]
-z input is NUL terminated
--missing allow missing objects
--batch allow creation of more than one tree
1 | # 待补充 |
git diff-tree
比较 2 个tree 对象 的差异并格式化输出。
usage: git diff-tree [–stdin] [-m] [-c] [–cc] [-s] [-v] [–pretty] [-t] [-r] [–root] [
-r diff recursively
–root include the initial commit as diff against /dev/null
common diff options:
-z output diff-raw with lines terminated with NUL.
-p output patch format.
-u synonym for -p.
–patch-with-raw
output both a patch and the diff-raw format.
–stat show diffstat instead of patch.
–numstat show numeric diffstat instead of patch.
–patch-with-stat
output a patch and prepend its diffstat.
–name-only show only names of changed files.
–name-status show names and status of changed files.
–full-index show full object name on index lines.
–abbrev=
-R swap input file pairs.
-B detect complete rewrites.
-M detect renames.
-C detect copies.
–find-copies-harder
try unchanged files as candidate for copy detection.
-l
-O
-S
–pickaxe-all
show all files diff when -S is used and hit is found.
-a –text treat all files as text.
1 | # 待补充 |
git gc
打包压缩操作,将多个 object 对象打包成 pack 文件对象。
命令说明(点击展开)
usage: git gc [<options>]
-q, --quiet suppress progress reporting
--prune[=<date>] prune unreferenced objects
--aggressive be more thorough (increased runtime)
--auto enable auto-gc mode
--force force running gc even if there may be another gc running
--keep-largest-pack repack all other packs except the largest pack
1 | $ git gc |
git verify-pack
查看 pack 包内容。
命令说明(点击展开)
usage: git verify-pack [-v | --verbose] [-s | --stat-only] <pack>...
-v, --verbose verbose
-s, --stat-only show statistics only
示例
1 | # 查看简略信息 |
git hash-object
计算并生成 object 文件。
命令说明(点击展开)
usage: git hash-object [-t <type>] [-w] [--path=<file> | --no-filters] [--stdin] [--] <file>...
or: git hash-object --stdin-paths
-t <type> object type
-w write the object into the object database
--stdin read the object from stdin
--stdin-paths read file names from stdin
--no-filters store file as is without filters
--literally just hash any random garbage to create corrupt objects for debugging Git
--path <file> process file as it were from this path
--literally
参数:可以对任意数据(可能是垃圾数据)创建松散对象,防止被 git 阻止或过滤。这种方式主要用于调试 git。
示例
1 | # 仅计算内容的 hash 值 |
git show-index
从标准输入(stdio)中读取打包索引文件(*.idx
)内容,解析并显示打包文件的索引信息。
命令说明(点击展开)
usage: git show-index
示例
1 | $ cat .git/objects/pack/pack-b433cf40a267c0fd6b9c4b8afff213eb9ef4a6fe.idx | git show-index |
git show-ref
列出本地引用(ref)。
命令说明(点击展开)
usage: git show-ref [-q | --quiet] [--verify] [--head] [-d | --dereference] [-s | --hash[=<n>]] [--abbrev[=<n>]] [--tags] [--heads] [--] [<pattern>...]
or: git show-ref --exclude-existing[=<pattern>]
--tags only show tags (can be combined with heads)
--heads only show heads (can be combined with tags)
--verify stricter reference checking, requires exact ref path
--head show the HEAD reference, even if it would be filtered out
-d, --dereference dereference tags into object IDs
-s, --hash[=<n>] only show SHA1 hash using <n> digits
--abbrev[=<n>] use <n> digits to display SHA-1s
-q, --quiet do not print results to stdout (useful with --verify)
--exclude-existing[=<pattern>]
show refs from stdin that aren't in local repository
1 | $ git show-ref |
git update-ref
更新引用(ref)的内容为指定的对象名称或 sha1 值。
命令说明(点击展开)
usage: git update-ref [<options>] -d <refname> [<old-val>]
or: git update-ref [<options>] <refname> <new-val> [<old-val>]
or: git update-ref [<options>] --stdin [-z]
-m <reason> reason of the update
-d delete the reference
--no-deref update <refname> not the one it points to
-z stdin has NUL-terminated arguments
--stdin read updates from stdin
--create-reflog create a reflog
1 | $ git update-ref HEAD 52855ba8f5c7e8410db2277ca1b00c4e1d1c2721 |
git rev-parse
把 revisions 解析成 commit-id ,除此之外,还有其他的功能。
命令说明(点击展开)
usage: git rev-parse --parseopt [<options>] -- [<args>...]
or: git rev-parse --sq-quote [<arg>...]
or: git rev-parse [<options>] [<arg>...]
1 | # 显示 revisions 的 sha1 值 |
git rev-list
按照时间顺序输出历史的 commit-id 。
命令说明(点击展开)
usage: git rev-list [OPTION] <commit-id>... [ -- paths... ]
limiting output:
--max-count=<n>
--max-age=<epoch>
--min-age=<epoch>
--sparse
--no-merges
--min-parents=<n>
--no-min-parents
--max-parents=<n>
--no-max-parents
--remove-empty
--all
--branches
--tags
--remotes
--stdin
--quiet
ordering output:
--topo-order
--date-order
--reverse
formatting output:
--parents
--children
--objects | --objects-edge
--unpacked
--header | --pretty
--[no-]object-names
--abbrev=<n> | --no-abbrev
--abbrev-commit
--left-right
--count
special purpose:
--bisect
--bisect-vars
--bisect-all
1 | # 显示 master 分支的历史版本 |
git for-each-ref
查看引用列表。
命令说明(点击展开)
usage: git for-each-ref [<options>] [<pattern>]
or: git for-each-ref [--points-at <object>]
or: git for-each-ref [(--merged | --no-merged) [<commit>]]
or: git for-each-ref [--contains [<commit>]] [--no-contains [<commit>]]
-s, --shell quote placeholders suitably for shells
-p, --perl quote placeholders suitably for perl
--python quote placeholders suitably for python
--tcl quote placeholders suitably for Tcl
--count <n> show only <n> matched refs
--format <format> format to use for the output
--color[=<when>] respect format colors
--sort <key> field name to sort on
--points-at <object> print only refs which points at the given object
--merged <commit> print only refs that are merged
--no-merged <commit> print only refs that are not merged
--contains <commit> print only refs which contain the commit
--no-contains <commit>
print only refs which don't contain the commit
--ignore-case sorting and filtering are case insensitive
1 | $ git for-each-ref |
git var
查看 Git 当前生效的变量。
命令说明(点击展开)
usage: git var (-l |
1 | $ git var -l |
git diff-tree
比较 2 个tree 对象 的差异并格式化输出。
命令说明(点击展开)
usage: git diff-tree [--stdin] [-m] [-c] [--cc] [-s] [-v] [--pretty] [-t] [-r] [--root] [<common-diff-options>] <tree-ish> [<tree-ish>] [<path>...]
-r diff recursively
--root include the initial commit as diff against /dev/null
common diff options:
-z output diff-raw with lines terminated with NUL.
-p output patch format.
-u synonym for -p.
--patch-with-raw
output both a patch and the diff-raw format.
--stat show diffstat instead of patch.
--numstat show numeric diffstat instead of patch.
--patch-with-stat
output a patch and prepend its diffstat.
--name-only show only names of changed files.
--name-status show names and status of changed files.
--full-index show full object name on index lines.
--abbrev=<n> abbreviate object names in diff-tree header and diff-raw.
-R swap input file pairs.
-B detect complete rewrites.
-M detect renames.
-C detect copies.
--find-copies-harder
try unchanged files as candidate for copy detection.
-l<n> limit rename attempts up to <n> paths.
-O<file> reorder diffs according to the <file>.
-S<string> find filepair whose only one side contains the string.
--pickaxe-all
show all files diff when -S is used and hit is found.
-a --text treat all files as text.
1 | git diff-tree a0e96b5ee9f1a3a73f340ff7d1d6fe2031291bb0^{tree} 523d41ce82ea993e7c7df8be1292b2eac84d4659^{tree} |
git merge-base
为合并找到共同祖先。
命令说明(点击展开)
usage: git merge-base [-a | --all] <commit> <commit>...
or: git merge-base [-a | --all] --octopus <commit>...
or: git merge-base --independent <commit>...
or: git merge-base --is-ancestor <commit> <commit>
or: git merge-base --fork-point <ref> [<commit>]
-a, --all output all common ancestors
--octopus find ancestors for a single n-way merge
--independent list revs not reachable from others
--is-ancestor is the first one ancestor of the other?
--fork-point find where <commit> forked from reflog of <ref>
1 |
git ref-log
查看引用变更日志。
命令说明(点击展开)
usage: git reflog [ show | expire | delete | exists ]
1 | $ git reflog |
git blame
显示文件的每一个修改版本和作者。
命令说明(点击展开)
usage: git blame [<options>] [<rev-opts>] [<rev>] [--] <file>
<rev-opts> are documented in git-rev-list(1)
--incremental Show blame entries as we find them, incrementally
-b Show blank SHA-1 for boundary commits (Default: off)
--root Do not treat root commits as boundaries (Default: off)
--show-stats Show work cost statistics
--progress Force progress reporting
--score-debug Show output score for blame entries
-f, --show-name Show original filename (Default: auto)
-n, --show-number Show original linenumber (Default: off)
-p, --porcelain Show in a format designed for machine consumption
--line-porcelain Show porcelain format with per-line commit information
-c Use the same output mode as git-annotate (Default: off)
-t Show raw timestamp (Default: off)
-l Show long commit SHA1 (Default: off)
-s Suppress author name and timestamp (Default: off)
-e, --show-email Show author email instead of name (Default: off)
-w Ignore whitespace differences
--indent-heuristic Use an experimental heuristic to improve diffs
--minimal Spend extra cycles to find better match
-S <file> Use revisions from <file> instead of calling git-rev-list
--contents <file> Use <file>'s contents as the final image
-C[<score>] Find line copies within and across files
-M[<score>] Find line movements within and across files
-L <n,m> Process only line range n,m, counting from 1
--abbrev[=<n>] use <n> digits to display SHA-1s
1 | $ git blame git-refs.md |
git check-attr
查看文件属性。
命令说明(点击展开)
usage: git check-attr [-a | –all |
or: git check-attr –stdin [-z] [-a | –all |
-a, --all report all attributes set on file
--cached use .gitattributes only from the index
--stdin read file names from stdin
-z terminate input and output records by a NUL character
1 | # 待补充 |
git count-objects
命令说明(点击展开)
usage: git count-objects [-v] [-H | –human-readable]
-v, --verbose be verbose
-H, --human-readable print sizes in human readable format
1 | $ git count-objects |
git fsck
校验对象链表的正确性和有效性。
命令说明(点击展开)
usage: git fsck [options] [object…]
-v, --verbose be verbose
--unreachable show unreachable objects
--dangling show dangling objects
--tags report tags
--root report root nodes
--cache make index objects head nodes
--reflogs make reflogs head nodes (default)
--full also consider packs and alternate objects
--connectivity-only check only connectivity
--strict enable more strict checking
--lost-found write dangling objects in .git/lost-found
--progress show progress
--name-objects show verbose names for reachable objects
1 | # 当前git仓库是正确的 |