References

A reference in git is probably the most important concept to understand. It’s fundamental to how git works internally, and how you modify the working of it day-to-day. Even if you’ve used git before but never heard of a ‘reference’, you’ve already used them.

You’ll often see them called refs in git internals.

A reference is a string that points to a commmit

There are four types of reference

  1. HEAD
  2. Tag
  3. Branch
  4. Remote

HEAD is a special reference, and it always tracks to the current position of the repository.

You can find out where HEAD points to by reading the file in the hidden .git directory

$ cat .git/HEAD
ref: refs/heads/master

Our current HEAD is pointing to master, which is a branch in this repository.

Every time you make a commit, the HEAD reference moves from the old commit to the new one.

Tag

A tag is simply a reference that points to a commit and that never changes, it stays in place for ever.

Even when HEAD changes, and we move on in time, the tag will remain at that specific commit.

git tag -a 1.2 -m 'April 2019 release'

$ git tag
v1.1
v1.2

Branches

A branch is a reference to a commit that moves as you add new commits. It’s like a tag, but it moves when the HEAD commit moves.

Do this, and see what happens when you do so:

$ mkdir git-branching
$ cd git-branching
$ git init
$ echo 1 > file
$ git add file
$ git commit -m "First commit"
$ git log --oneline --decorate --all --graph
$ git branch branch_a
$ git tag first_tag
$ git log --oneline --decorate --all --graph
$ echo 2 >> file
$ git commit -am "Second commit"
$ git checkout branch_b
$ git log --oneline --decorate --all --graph
$ echo 3 >> file
$ git commit -am "Third commit"
$ git log --oneline --decorate --all --graph

Remotes

A remote reference refers to commits that are outside of the local git repository on another machine. The most obvious example of this is Github/BitBucket origin servers. The remote reference can point to the same branch as the one you’re on and tracking, and so know if the code on the origin server needs to be pulled in. We’ll explore this more later.


Now we’ve understood a bit more about references, let’s go to writing a good commit message