The history in Git is formed from the commit
objects; as development advances, branches are created and merged, and the history will create a directed acyclic graph, the DAG, because of the way that Git ties a commit to its parent commit. The DAG makes it easy to see the development of a project based on the commits.
Please note that the arrows in the following diagram are dependency arrows, meaning that each commit points to its parent commit(s), which is why the arrows point in the opposite direction to the normal flow of time:
A graph of the example repository with abbreviated commit IDs
You can view the history (the DAG) in Git by using its git log
command. There are also a number of visual Git tools that can graphically display the history. This section will show some features of git log
.
We will use the example repository from the last section and ensure that the master branch is pointing to 34acc37
:
$ git checkout master && git reset --hard 34acc37
In the previous command, we only use the first seven characters (34acc37
) of the commit ID; this is fine as long as the abbreviated ID that is used is unique in the repository.
$ git log -3
- This will display the following result:
commit 34acc370b4d6ae53f051255680feaefaf7f7850d
Author: John Doe <[email protected]>
Date: Fri Dec 13 12:26:00 2013 +0100
This is the subject line of the commit message.
It should be followed by a blank line then the body, which is this text. Here
you can have multiple paragraphs etc. and explain your commit. It's like an
email with subject and body, so try to get people's attention in the subject
commit a90d1906337a6d75f1dc32da647931f932500d83
Author: John Doe <[email protected]>
Date: Fri Dec 13 12:17:42 2013 +0100
Instructions for compiling hello_world.c
commit 485884efd6ac68cc7b58c643036acd3cd208d5c8
Merge: 44f1e05 0806a8b
Author: John Doe <[email protected]>
Date: Fri Dec 13 12:14:49 2013 +0100
Merge branch 'feature/1'
Adds a hello world C program.
- By default,
git log
prints the commit, author's name and email ID, timestamp, and the commit message. However, the information isn't very graphical, especially if you want to see branches and merges. To display this information and limit some of the other data, you can use the following options withgit log
:
$ git log --decorate --graph --oneline --all
- The previous command will show one commit per line (
--oneline
), identified by its abbreviated commit ID, and the commit message subject. A graph will be drawn between the commits depicting their dependency (--graph
). The--decorate
option shows the branch names after the abbreviated commit ID, and the--all
option shows all the branches, instead of just the current one(s):
$ git log --decorate --graph --oneline --all
* 34acc37 (HEAD, tag: v1.0, origin/master, origin/HEAD, master) This is the sub...
* a90d190 Instructions for compiling hello_world.c
* 485884e Merge branch 'feature/1'
...
This output, however, gives neither the timestamp nor the author information, because of the way the --oneline
option formats the output.
- Fortunately, the
log
command gives us the ability to create our own output format. So, we can make a history view similar to the previous one. The colors are made with the%C<color-name>text-be-colored%Creset
syntax, along with the author and timestamp information and some colors to display it nicely:
$ git log --all --graph \ --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%ci) %C(bold blue)<%an>%Creset'
- This is a bit cumbersome to write, but luckily, it can be made as an alias so you only have to write it once:
git config --global alias.graph "log --all --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%ci) %C(bold blue)<%an>%Creset'"
Git traverses the DAG by following the parent IDs (hashes) from the given commit(s). The options passed to git log
can format the output in different ways; this can serve several purposes—for example, to give a nice graphical view of the history, branches, and tags, as seen previously, or to extract specific information from the history of a repository to use, for example, in a script.