Linux command
git-rev-parse 命令
文本
复制后可按需替换文件名、目录或参数。
常用示例
Get commit hash
git rev-parse HEAD
Get short hash
git rev-parse --short HEAD
Get branch name
git rev-parse --abbrev-ref HEAD
Get repository root
git rev-parse --show-toplevel
Check if in git repository
git rev-parse --is-inside-work-tree
Get git directory
git rev-parse --git-dir
Verify
git rev-parse --verify [ref]
Get path
git rev-parse --show-prefix
说明
git rev-parse translates ref names, symbolic references, and other arguments into SHA-1 hashes. It also provides information about the repository structure, such as the root directory, git directory, and current branch. This plumbing command is widely used in scripts for resolving refs and querying repository metadata.
参数
- --short _length_
- Abbreviated hash.
- --abbrev-ref
- Show short ref name.
- --verify
- Verify object exists.
- --show-toplevel
- Show repository root.
- --git-dir
- Show .git directory.
- --is-inside-work-tree
- Check if in work tree.
- --show-prefix
- Show path of current directory relative to repository root.
- --show-cdup
- Show path from current directory to repository root.
- --is-bare-repository
- Print "true" if the repository is bare, "false" otherwise.
- --absolute-git-dir
- Like --git-dir, but output is always an absolute path.
- --symbolic
- Show symbolic name instead of SHA-1.
- --symbolic-full-name
- Like --symbolic but show full ref name (e.g., refs/heads/master).
- --quiet
- Only meaningful with --verify. Do not output error message on failure, just exit with non-zero status.
FAQ
What is the git-rev-parse command used for?
git rev-parse translates ref names, symbolic references, and other arguments into SHA-1 hashes. It also provides information about the repository structure, such as the root directory, git directory, and current branch. This plumbing command is widely used in scripts for resolving refs and querying repository metadata.
How do I run a basic git-rev-parse example?
Run `git rev-parse HEAD` in a terminal, then adjust file names, paths, flags, or remote targets for your system.
What does --short _length_ do in git-rev-parse?
Abbreviated hash.