Programming and IT
Git branches
A Git branch is a movable pointer to a commit, not a copy of your files. That is why creating one costs nothing, and the git branch command is what you use to list, create, rename and delete branches.
In this article
A branch is a pointer#
Inside the repository a branch is stored as a single line holding a commit hash.
When you make a new commit, the pointer of the current branch moves to it — and
only that pointer. A separate pointer called HEAD remembers which branch you
are on right now.
Everything else follows from this. Creating a branch is instant, no matter how many files the project has. Ten branches do not take ten times the space. And deleting a branch does not delete commits: only the name disappears, the commits stay as long as something still refers to them.
See what you have#
feature/login
* main
hotfix-404
The asterisk marks the current branch. Useful variations of the listing:
| Command | What it shows |
|---|---|
git branch -v |
the latest commit of every branch on one line |
git branch -vv |
the same plus the tracked branch on the server |
git branch -a |
both local and remote branches |
git branch -r |
remote branches only (origin/…) |
git branch --show-current |
just the current name — handy in scripts |
git branch --merged |
branches already merged into the current one: candidates for deletion |
Create, switch, delete#
The branch exists now, but you are still where you were. Usually you want to move to it straight away:
Switched to a new branch 'feature/export'
The switch and restore commands appeared in Git 2.23 to split up the job of
the old checkout: it could both change branches and restore files, which made
it easy to use in the wrong way. The old form git checkout -b feature/export
still works — see git checkout for the details.
Deleting:
Deleted branch feature/export (was 9d31c07).
Lowercase -d refuses to delete a branch whose work has not been merged
anywhere — error: the branch 'feature/export' is not fully merged. That is a
safeguard, not a bug: make sure you really do not need those commits and repeat
with uppercase -D. This does not touch the branch on the server; you delete
that one with a push — see git push.
Detached HEAD#
If you switch not to a branch but to a specific commit or tag, Git says:
Note: switching to '4f1c2ab'.
You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them...
Nothing bad has happened: you are simply looking at an older state of the
project, and HEAD points straight at a commit instead of at a branch. There is
only one danger — commits you make in this state do not belong to any branch, and
after you switch away they will have no name.
How you get out depends on what you did. If you made no commits, go back to the branch you were on:
If you made commits and you need them, give them a name right where you are:
If you already left and the commits were left without a name, find their hash in
git reflog and create a branch from it: git branch rescue 3c9f011.
Renaming and keeping the list tidy#
Without the first name, the current branch gets renamed — this is how people
change master to main in an old repository. After a rename the branch on the
server keeps its old name, so you have to push the new one and delete the old
one there.
Every couple of weeks it pays to clean up the list: git branch --merged shows
merged branches, and git fetch --prune removes remote branches from your local
list once they no longer exist on the server. Keep names descriptive —
feature/export-csv, fix/login-500 — and the branch list reads like a to-do
list rather than a jumble of letters. The other everyday commands are collected
in the Git commands cheat sheet.
Step-by-step plan
- Check where you aregit status or git branch --show-current before any switch.
- Create a branch from the right placegit switch -c name; give a starting point if needed: git switch -c name main.
- Work and commitCommits move only this branch's pointer; main stays where it was.
- Merge the workGo back to main and merge the branch; after that git branch --merged lists it.
- Delete the finished namegit branch -d name locally and git push origin --delete name on the server.
Start learning this in your own space
The plan goes into your repository: tick off stages, keep notes — the change history shows how far you have come.
Check yourself
1.Which command creates a branch called feature and switches to it at once?
2.Git said “You are in 'detached HEAD' state” and you have already made commits. What keeps them?
3.Which command lists the branches already merged into the current one?
Sources
-
Pro Git bookThe chapter “Git Branching” with diagrams of the pointersfree
-
git branch referenceOptions for listing, creating and deletingfree
-
git switch referenceThe modern command for switching branchesfree
Was this helpful?