Sub topic 2
A branch is a pointer to a commit
This chapter assumes you can make commits, read git log, and run git status. It builds the working model that the rest of the tutorial relies on. A branch is not a copy of your files and not a container of commits. It is a small named reference to one commit.
What a branch really is
Every commit stores a snapshot of your project, its author and message, and the ID of its parent commit (or parents, for a merge). Following parent links from any commit gives you that commit's history. A branch is a name that points at one commit. Git calls the commit a branch points to the branch's tip.
Inside the repository, a branch is a file. For a branch named main, Git stores the commit ID in .git/refs/heads/main. The file contains a 40-character SHA-1 hash (or 64 characters in a SHA-256 repository), plus a newline. You can check this yourself.
cat .git/refs/heads/main
git rev-parse mainBoth commands print the same ID. If you have run git pack-refs or git gc, the branch may live in .git/packed-refs instead of its own file. Use git rev-parse when you want a command that works in either case.
Because a branch is only an ID in a small file, creating one writes about 41 bytes. It does not copy any project files. That is why branching in Git is fast, even in a very large repository.
How the pointer moves
A branch name moves forward when you commit while that branch is checked out. Git creates the new commit with the current tip as its parent, then updates the branch to point to the new commit. The old commits stay where they are.
Arrows point from a commit to its parent, because that is how Git stores the link. A commit knows its parent, but a parent does not know its children. Branch names are the only things that give you a way to reach the newest commit.
Two branches on one commit
Create a branch and you add a second name for the same commit. Nothing diverges until one of the branches gets a new commit.
git branch feature/login
git log --oneline --decorate -3The output looks like this, with your own IDs:
c3f9a1e (HEAD -> main, feature/login) Add password reset link
7b2d4c0 Add login form
91e5a8f Initial commitHEAD -> main shows that main is the branch you are on. feature/login points at the same commit as main. Now switch to the new branch and commit.
git switch feature/login
echo "validation" > validate.txt
git add validate.txt
git commit -m "Add input validation"Git creates a new commit whose parent is c3f9a1e. Only feature/login moves to it. main stays on c3f9a1e. The history now has two tips, and the branches have diverged only in the sense that their pointers differ.
Inspecting where branches point
These commands show you the pointers directly. Run them often while you learn.
git branch -v
git log --oneline --graph --decorate --all
git rev-parse main feature/login
git show-ref --headsgit branch -vlists local branches with the tip commit of each.git log --graph --decorate --alldraws the whole graph and labels each tip. Without--all,git logshows only what is reachable from HEAD.git rev-parseturns a name into a full commit ID.git show-ref --headslists every local branch and its ID, including packed ones.
Branch names are not permanent labels
A branch has no identity apart from its name and the commit it points at. Git does not record which branch a commit was made on. If you merge feature/login into main and delete the branch name, every commit remains in the history. Only the label is gone.
This has practical effects:
- Moving a branch is cheap and safe.
git branch -f name <commit>repoints a branch you are not on, and the commits it used to point at remain in the repository. - Two branches can point at the same commit, and the commit does not belong to either one.
- A commit is "on" a branch only in the sense that it is reachable by following parent links from the tip.
git branch --contains <commit>lists the branches from which a commit is reachable.
Reachability and garbage collection
Git keeps a commit as long as something can reach it. Branches, tags, remote-tracking branches, and the reflog all count as ways to reach a commit. A commit that nothing reaches is unreachable. Git does not delete it right away. Garbage collection removes unreachable objects only after a grace period. By default that is two weeks for unreachable commits, and 90 days for commits that the reflog still records.
This is why deleting a branch is less dangerous than it sounds, and why a later chapter can recover work with the reflog. It is also the reason a branch matters. Commits that no name reaches will eventually disappear.
Common errors
- Thinking a branch holds a copy of the files. Switching branches changes the files in your working directory to match the new tip. The branch itself stores only the ID.
- Expecting
git branch nameto switch to it. It creates the pointer and leaves HEAD where it was. Usegit switch name, orgit switch -c nameto create and switch in one step. - Committing to the wrong branch. Run
git branch --show-currentbefore you commit if you are unsure. The commit goes to whichever branch HEAD names. - Reading
git logand assuming it shows every branch. It shows the history reachable from HEAD only. Add--allto include others. - Assuming a commit "belongs" to the branch it was made on. Git has no such record. Reachability decides.
Exercise
You are on main, which points at commit c3f9a1e. You run git branch hotfix and then make one commit. Which branch name points at the new commit, and where does hotfix point?
Show answer
main points at the new commit, because git branch hotfix only creates a pointer and does not switch to it, so HEAD still names main and the commit moves main forward. hotfix still points at c3f9a1e.
Exercise
You run git branch -f main <older-commit-id> while you are on another branch. Are the commits that main used to contain deleted immediately? What would you check first to avoid losing them?
Show answer
No. Moving the pointer deletes nothing, and the old commits stay in the repository until garbage collection removes them. Before moving it, record the old tip with git rev-parse main, or confirm that another branch or tag reaches those commits, for example with git branch --contains <commit>.