Git Rev-Parse
Overview
Git rev-parse helps massage option (Massage just implies that it is possible to convert the info from one form into another i.e. a transformation command.) to bring similar facilities as C builtins have to shell script.rev-parse helps to find the top-level directory in your git worktree, the hash value of the commit, and also to check the current location.
Pre-Requisites
Before knowing about rev-parse commands and their uses you should be aware of the basics of Git, Git branches, Git commits.
Introduction to Git rev-parse
Git rev-parse is a subcommand used for the following activities:
- To print the hash values of the commits.
- To validate whether the git object is valid or non-valid.
- Displaying the absolute or relative path of the .git directory.
- To check if you are currently in a repository or inside a worktree.
- Checking if the repo is bare (A bare Git repository is typically used as a Remote Repository that is sharing a repository among several different people.)
- Displaying SHA1 hash values of branches (--branches), tags (--tags), and the refs can also be filtered based on the remote (using --remote)
Synopsis
**rev-parse can be used by the command: **
The rev-parse command takes a mixture of flags(parameters that starts with a dash -). These flags tell the git-rev parse to perform the operation in a particular manner depending on the type of flag used. <options> are additional flags used with rev-parse to perform other tasks.
Options
Some of the options which are used with rev-parse are:
-
--parseopt This command uses Git rev-parse in option parsing mode. In --parseopt mode, rev-parse helps to massage options and bring the C builtins like the facility to shell script.
-
--keep-dashdash This command runs only with --parseopt mode. It tells the option parser to read the -- (dashdash) encountered in the command instead of skipping it.
-
--revs-only This command is used for option filtering. It does not output flags and parameters that are made for the git rev-list command.
-
--no-revs This command is used for option filtering. It does not output flags and parameters that are not made for the git rev-list command.
-
--default
When no parameter is given by the user then use the . -
--short[=length] This command shortens the object name to a unique prefix with at least length(passed to short) characters.
-
--show-toplevel This command shows the top-level directory of the Git working tree. If no working tree is found then it throws an error.
-
--show-cdup This command is used to know the number of levels you need to get to the root directory.
You can learn about many other options rev-parse provides from the official documentation of Git.
Using Rev-Parse
We can use the rev-parse command to get information about our working Git repository.
Getting the Top-Level Directory.
Git rev-parse provides an option to get the git top-level directory in your current working repository. Git stores all its information in a .git file and only uses it to know about all the tracked files. The --show-top level gives the root directory of your current Git repository.
This command becomes useful when you are working in a large directory and want to know the root directory of the big repository. Even if you are present in a deeply nested subfolder you will be able to know the root directory.
Finding Your Way Home to the Top-Level Directory
If you are present in a nested directory and want to know the number of levels you want to get to the top level then you can use the option --show-cdup. It is a more easy solution to get out of the nested folder instead of reverse engineering the folder structure.
Current Location
You can use the --is-inside-work-tree and --is-inside-git-dir options to know if you are executing the command from the required directory. This option returns a boolean value.
Conclusion
- rev-parse helps massage option to bring to shell script the same facility C builtins have.
- rev-parse can print the SHA hashes of the commit.
- rev-parse can verify a valid git object using the --verify option.
- Using rev-parse you can check if you are currently in a directory or worktree.
- rev-parse can display the top-level directory in the .git file.