Close

Git - Understanding HEAD

[Updated: Mar 21, 2019, Created: Mar 20, 2019]

In Git, HEAD is a special pointer to keep track what branch we are currently in.

Example

Let's start Git bash for windows and add a project to Git:

joe@jpc MINGW64 /d
$ mkdir example-project

joe@jpc MINGW64 /d
$ cd example-project/

joe@jpc MINGW64 /d/example-project
$ git init
Initialized empty Git repository in D:/example-project/.git/

joe@jpc MINGW64 /d/example-project (master)
$ echo a > test.txt

joe@jpc MINGW64 /d/example-project (master)
$ git add -A && git commit -m "adding test.txt"
[master (root-commit) 04d3c0c] adding test.txt
 1 file changed, 1 insertion(+)
 create mode 100644 test.txt

To find out where the HEAD is pointing to, we can use git log --decorate --oneline:

joe@jpc MINGW64 /d/example-project (master)
$ git log --decorate --oneline
04d3c0c (HEAD -> master) adding test.txt

Let's create a new branch and make some changes to it:

joe@jpc MINGW64 /d/example-project (master)
$ git checkout -b xyz
Switched to a new branch 'xyz'

joe@jpc MINGW64 /d/example-project (xyz)
$ echo b >> test.txt

joe@jpc MINGW64 /d/example-project (xyz)
$ git add -A && git commit -m "modifying test.txt"
[xyz d3894c5] modifying test.txt
 1 file changed, 1 insertion(+)

joe@jpc MINGW64 /d/example-project (xyz)
$ echo z > test2.text

joe@jpc MINGW64 /d/example-project (xyz)
$ git add -A && git commit -m "adding test2.txt"
[xyz 5ca5247] adding test2.txt
 1 file changed, 1 insertion(+)
 create mode 100644 test2.text

joe@jpc MINGW64 /d/example-project (xyz)
$ git log --decorate --oneline
5ca5247 (HEAD -> xyz) adding test2.txt
d3894c5 modifying test.txt
3c5ced9 (master) adding test.txt

Now switch back to master and make some changes there:

joe@jpc MINGW64 /d/example-project (xyz)
$ git checkout master
Switched to branch 'master'

joe@jpc MINGW64 /d/example-project (master)
$ git log --decorate --oneline
04d3c0c (HEAD -> master) adding test.txt

joe@jpc MINGW64 /d/example-project (master)
$ echo x > test3.txt

joe@jpc MINGW64 /d/example-project (master)
$ git add -A && git commit -m "adding test3.txt"
[master 37ab536] adding test3.txt
 1 file changed, 1 insertion(+)
 create mode 100644 test3.txt

joe@jpc MINGW64 /d/example-project (master)
$ git log --decorate --oneline
37ab536 (HEAD -> master) adding test3.txt
3c5ced9 adding test.txt

Using --graph option

To have a text-based graphical representation of the commit history we can use git log --decorate --oneline --all --graph

joe@jpc MINGW64 /d/example-project (master)
$ git log --decorate --oneline --all --graph
* 37ab536 (HEAD -> master) adding test3.txt
| * 5ca5247 (xyz) adding test2.txt
| * d3894c5 modifying test.txt
|/
* 3c5ced9 adding test.txt

joe@jpc MINGW64 /d/example-project (master)
$ git checkout xyz
Switched to branch 'xyz'

joe@jpc MINGW64 /d/example-project (xyz)
$ git log --decorate --oneline --all --graph
* 37ab536 (master) adding test3.txt
| * 5ca5247 (HEAD -> xyz) adding test2.txt
| * d3894c5 modifying test.txt
|/
* 3c5ced9 adding test.txt

See Also