Skip to main content
  1. Learn
  2. Software Development
  3. Guides
  4. Git tutorial
  5. Git collaboration
  6. Switching branches
  7. Pointing to branches
GuidesSoftware DevelopmentBacklog
Git

Project and code management together.

Pointing to branches

In Git, "pointing to branches" refers to the concept of references or pointers that Git uses to keep track of different states of your project's codebase.

Here’s a clear explanation of what it means when we talk about pointing to branches:

  1. HEAD pointer:
    • Git maintains a special pointer called HEAD that points to the currently checked-out branch or commit.
    • When you switch branches, Git moves HEAD to point to the latest commit of the new branch.
  2. Branch pointers:
    • Each branch in Git is essentially a pointer (or reference) to a specific commit in your repository's history.
    • When you create a new branch, Git creates a new pointer that initially points to the same commit as the branch you created it from.
  3. Commit history:
    • The commits in Git form a linked list where each commit points to its parent commit(s).
    • Branches are maintained by moving these pointers to different commits as new commits are made, effectively changing the state of the branch.

HEAD is used to represent the current snapshot of a branch. For a new repository, Git will, by default, point HEAD to the main branch. Changing where HEAD is pointing to will update your active branch.

The ~(tilde) and ^(caret) symbols point to a position relative to a specific commit. The symbols are used with a commit reference, typically HEAD or a commit hash.

  • ~ refers to ancestors (how many generations back depends on the number).
  • HEAD~1 refers to the commit’s first parent.
  • HEAD~2 refers to the commit’s first grandparent.
  • ^ refers to parents of merge commits.
  • HEAD^1 refers to the first parent of HEAD where head is a merge commit.
  • HEAD^2 refers to the first grandparent of HEAD where head is a merge commit.

A commit can have two parents in a merge commit.

Diagram of git symbols pointing to specific positions.
~(tilde) and ^(caret) symbols point to a position relative to the commit.

Subscribe to our newsletter

Learn with Nulab to bring your best ideas to life