Branches are Pointers to Commits - neverendingqs/git-commit-it GitHub Wiki

Branches can act like bookmarks for commits you would like to track.

Notes:

  • I will be using aliases:
    • git tree: a replacement for SourceTree or gitk for getting a visual view of your branches
  • Depending on the state of your repository, instead of opening files in .git/refs/remotes/origin/<name> for the demo, you may need to run git log origin/<name> to get the commit hash

Demo

  • Create a new branch named michelle that points to origin/master:
    • git branch michelle origin/main
  • Open up the following files:
    • .git/refs/heads/michelle
    • .git/refs/remotes/origin/main
  • They should both have the same contents, which is a hash string of a specific commit
  • Copy the hash string, and run the following to confirm it exists:
    • git log <hash>
  • Open up the following files:
    • .git/refs/heads/michelle
    • .git/refs/remotes/origin/all-you-wanted
    • .git/refs/remotes/origin/everywhere
    • .git/refs/remotes/origin/game-of-love
  • Notice that their contents are all different
  • For each .git/refs/remotes/origin/* file
    • Replace .git/refs/heads/michelle's hash with its hash
    • Run git tree and observe that michelle branch now points to the the same commit as the file from where you copy its hash

Note: although we changed where the branch pointed to, Git didn't make the corresponding changes to our working directory. Use git checkout or git switch to change branches.

Note: a branch points to the latest commit of that branch. It only points to one commit, but it's useful for these demos to think of a commit being on a branch if a branch points to it or a "child" commit.

Summary

Branches are pointers to a commit in your repository.

⚠️ **GitHub.com Fallback** ⚠️