It is fairly common to have some scripts that use repository information, for example, builds or release note generation. This small example will show some examples of the rev-parse
command that can be very useful for scripting.
Clone the data-model
repository from Chapter 1, Navigating Git:
$ git clone https://github.com/dvaske/data-model.git $ cd data-model
First, let's figure out the ID of the commit at HEAD
:
$ git rev-parse HEAD 34acc370b4d6ae53f051255680feaefaf7f7850d
This can, of course, also be obtained by git log -1 --format=%H
, but with the rev-parse
command, you don't need all the options. We can also get the current branch from the rev-parse
command:
$ git rev-parse --symbolic-full-name HEAD refs/heads/master
We can also just get the abbreviated name:
$ git rev-parse --symbolic-full-name --abbrev-ref HEAD master
We can also get the ID of other refs:
$ git rev-parse origin/feature/2 82cd5662900a50063ff4bb790539fe4a6d470d56
The rev-parse
command can also be used to give some information about the repository, especially the directory structure relative to the current working directory.
We can find the top-level directory from a subdirectory using the following snippet:
$ cd a_sub_directory $ pwd /path/to/repo/data-model/a_sub_directory $ git rev-parse --show-toplevel /path/to/repo/data-model
We can also get the number of cd ..
we need to get to the root directory using the following command:
$ git rev-parse --show-cdup ../
And we can get the relative path from the root directory to the current working directory using the following command:
$ git rev-parse --show-prefix a_sub_directory/
Finally, we can get Git to show the path to the .git
directory:
$ git rev-parse --git-dir /path/to/repo/data-model/.git
We can check whether the current repository is a bare repository using the following command:
$ git rev-parse --is-bare-repository false