Blog

Git References, Commits, and Branches
Posted on August 30, 2020 in Git by Matt Jennings

References

They are pointers to commits.

Three Types of Git References

  • Tags and Annotated Tags
  • Branches
  • HEAD

What is Head?

  • HEAD is how git knows what branch you’re currently on, and what the next parent will be.
  • It’s a pointer.
    • It usually points at the name of the current branch.
    • But, it can point at a commit too (detached HEAD).

Lightweight Tags (in practice not used much)

  • Lightweight tags are just a simple pointer to a commit.
  • When you create a tag with no arguments, it captures the value in HEAD.
  • Example:
    git checkout master
    git tag my-first-commit

Annotated Tags: git tag -a

  • Point to a commit, but store additional information.
    • Author, message, date.
  • Example:
    git tag -a v1.0 -m 'Version 1.0 of my blog'
    git tag
    git show v1.0

Tags and Branches

  • Branch
    • The current branch pointer moves with every commit to the repository
  • Tag
    • The commit that a tag points doesn’t change.
    • It’s a snapshot!

HEAD-less / Detached HEAD

  • When I checked a checkout a specific commit or tag, I go into a detached HEAD state.
  • git move the HEAD point to that commit.
  • There is no reference pointing to the commits you made in a detached state.
  • Save your work:
    • Create a new branch that points to the last commit you made in a detached state:
      git branch <new-branch-name> <commit>

Dangling Commits

  • If you don’t point a new branch at those detached HEAD commits, they will not longer be referenced in git.
  • Eventually they will be garbage collected.

 

Leave a Reply