Programming Search⌘K

Featurecore

git branch

Also called branch, branching, line of development, bookmark, codeline.

A branch is a line of development in a version control system. In Git, a branch is a name that points to one commit, and the name moves to each commit that you make on it.

Tool
Git

Synopsis

Work on a project often splits. One person fixes a bug while another builds a feature, and both start from the same commit. A branch is one of these lines of development. Version control systems store branches in two main ways. Some record the branch name in each commit, so the name is part of the history forever. Others keep the name outside the commits, as a pointer that can move or go away. Git uses pointers.1 2

git branch                       list the local branches; * marks the current one
git branch <name> [<start>]      make a branch at <start> (default HEAD); do not switch to it
git switch <name>                make <name> the current branch
git switch -c <name> [<start>]   make a branch and switch to it
git switch --detach <commit>     check out a commit with no current branch
git branch -d <name>             delete a branch that is merged
git branch -D <name>             delete a branch even when it is not merged
git branch -m <old> <new>        rename a branch
git branch -v | -vv              show the tip of each branch, and its upstream with -vv
git branch --show-current        print the name of the current branch
git branch -u <remote>/<branch>  set the upstream of the current branch

git checkout <name> and git checkout -b <name> are the older forms of git switch and git switch -c.4

Semantics

r1. A branch is a ref under refs/heads/. A ref is a name that holds the name of one commit. That commit is the tip of the branch.2 16 Example: pointer.

r2. Making a branch writes one ref. It copies no file and no commit. Two branches can point to the same commit.1 Example: pointer.

r3. HEAD names the current branch. It is a symbolic ref: it holds the name of a branch, such as ref: refs/heads/main, not a commit.3 16 Example: pointer.

r4. When you commit, Git moves the branch that HEAD names to the commit. Other branches stay where they are.2

r5. The commits of a branch are all commits that its tip can reach through parent links. So a commit can be on many branches at once. When two branches have different commits, they have diverged. A merge base of the two is a common ancestor that no other common ancestor descends from.7 Example: diverge.

r6. A..B means the commits that B can reach and A cannot. So git rev-list main..topic lists the commits that are on topic and not yet on main.8 Example: diverge.

r7. When HEAD holds a commit name instead of a branch name, HEAD is detached. Commits that you make there belong to no branch. When you switch away, only the reflog still names them.5 6 Example: detached.

r8. git branch -d deletes a branch only when its tip is merged into its upstream, or into HEAD when it has no upstream. -D deletes it anyway. Deleting a branch deletes the ref, not the commits.9 Example: delete.

r9. A remote-tracking branch, such as origin/main, records where a branch of another repository was at the last fetch or push. The upstream of a local branch is the branch that it merges from or rebases onto by default. git status compares the local branch with its upstream.10 11 Example: upstream.

Examples

Each example starts in an empty directory. The export lines fix the identity and the dates, so the hashes on this page are the hashes that you get.

pointer

export GIT_CONFIG_GLOBAL=/dev/null GIT_CONFIG_NOSYSTEM=1
export GIT_AUTHOR_NAME=Ada GIT_AUTHOR_EMAIL=ada@example.com GIT_AUTHOR_DATE=2026-01-05T10:00:00Z
export GIT_COMMITTER_NAME=Ada GIT_COMMITTER_EMAIL=ada@example.com GIT_COMMITTER_DATE=2026-01-05T10:00:00Z
exec 2>&1
git -c init.defaultBranch=main init -q shop && cd shop
git --version
printf 'apples\n' > list.txt
git add list.txt
git commit -q -m "Add the shopping list"
git branch topic
cat .git/HEAD
cat .git/refs/heads/main
cat .git/refs/heads/topic
git branch
git branch --show-current
Output
git version 2.56.0
ref: refs/heads/main
c6fb135760531277211142d3556246d6d0d6c46d
c6fb135760531277211142d3556246d6d0d6c46d
* main
  topic
main

Each branch file holds the 40 digits of one commit name and a newline. HEAD holds the name of a branch.

diverge

export GIT_CONFIG_GLOBAL=/dev/null GIT_CONFIG_NOSYSTEM=1
export GIT_AUTHOR_NAME=Ada GIT_AUTHOR_EMAIL=ada@example.com GIT_AUTHOR_DATE=2026-01-05T10:00:00Z
export GIT_COMMITTER_NAME=Ada GIT_COMMITTER_EMAIL=ada@example.com GIT_COMMITTER_DATE=2026-01-05T10:00:00Z
exec 2>&1
git -c init.defaultBranch=main init -q shop && cd shop
printf 'apples\n' > list.txt
git add list.txt
git commit -q -m "Add the shopping list"
git switch -c topic
printf 'bread\n' >> list.txt
git commit -q -a -m "Add bread"
git switch main
printf 'Shopping\n' > README
git add README
git commit -q -m "Add a README"
git log --graph --oneline --decorate --all
git merge-base main topic
git rev-list --count main..topic
Output
Switched to a new branch 'topic'
Switched to branch 'main'
* 8489f80 (HEAD -> main) Add a README
| * d54235d (topic) Add bread
|/
* c6fb135 Add the shopping list
c6fb135760531277211142d3556246d6d0d6c46d
1

The two branches share c6fb135, which is their merge base. topic has one commit that main does not have.

detached

export GIT_CONFIG_GLOBAL=/dev/null GIT_CONFIG_NOSYSTEM=1
export GIT_AUTHOR_NAME=Ada GIT_AUTHOR_EMAIL=ada@example.com GIT_AUTHOR_DATE=2026-01-05T10:00:00Z
export GIT_COMMITTER_NAME=Ada GIT_COMMITTER_EMAIL=ada@example.com GIT_COMMITTER_DATE=2026-01-05T10:00:00Z
exec 2>&1
git -c init.defaultBranch=main init -q shop && cd shop
printf 'apples\n' > list.txt
git add list.txt
git commit -q -m "Add the shopping list"
git switch --detach HEAD
printf 'bread\n' >> list.txt
git commit -q -a -m "Add bread"
git switch main
git branch rescue HEAD@{1}
git log --oneline rescue
Output
HEAD is now at c6fb135 Add the shopping list
Warning: you are leaving 1 commit behind, not connected to
any of your branches:

  d54235d Add bread

If you want to keep it by creating a new branch, this may be a good time
to do so with:

 git branch <new-branch-name> d54235d

Switched to branch 'main'
d54235d Add bread
c6fb135 Add the shopping list

HEAD@{1} is the place where HEAD was one move ago. The branch rescue gives the lost commit a name again.

delete

export GIT_CONFIG_GLOBAL=/dev/null GIT_CONFIG_NOSYSTEM=1
export GIT_AUTHOR_NAME=Ada GIT_AUTHOR_EMAIL=ada@example.com GIT_AUTHOR_DATE=2026-01-05T10:00:00Z
export GIT_COMMITTER_NAME=Ada GIT_COMMITTER_EMAIL=ada@example.com GIT_COMMITTER_DATE=2026-01-05T10:00:00Z
exec 2>&1
git -c init.defaultBranch=main init -q shop && cd shop
printf 'apples\n' > list.txt
git add list.txt
git commit -q -m "Add the shopping list"
git switch -q -c topic
printf 'bread\n' >> list.txt
git commit -q -a -m "Add bread"
git switch -q main
git branch -d topic || echo "exit status $?"
git branch -D topic
git cat-file -t d54235d
Output
error: the branch 'topic' is not fully merged
hint: If you are sure you want to delete it, run 'git branch -D topic'
hint: Disable this message with "git config set advice.forceDeleteBranch false"
exit status 1
Deleted branch topic (was d54235d).
commit

The commit d54235d still exists after the delete. No branch names it now, so a later git gc can remove it when the reflog entries expire.

upstream

export GIT_CONFIG_GLOBAL=/dev/null GIT_CONFIG_NOSYSTEM=1
export GIT_AUTHOR_NAME=Ada GIT_AUTHOR_EMAIL=ada@example.com GIT_AUTHOR_DATE=2026-01-05T10:00:00Z
export GIT_COMMITTER_NAME=Ada GIT_COMMITTER_EMAIL=ada@example.com GIT_COMMITTER_DATE=2026-01-05T10:00:00Z
exec 2>&1
git -c init.defaultBranch=main init -q shop && cd shop
printf 'apples\n' > list.txt
git add list.txt
git commit -q -m "Add the shopping list"
git -c init.defaultBranch=main init -q --bare ../origin.git
git remote add origin ../origin.git
git push -q -u origin main
printf 'bread\n' >> list.txt
git commit -q -a -m "Add bread"
git status -sb
git branch -vv
Output
## main...origin/main [ahead 1]
* main d54235d [origin/main: ahead 1] Add bread

Versions

Git 2.23 added git switch for branch work and git restore for file work. Before it, git checkout did both.12 Git 2.22 added git branch --show-current.15 Git 2.28 added the setting init.defaultBranch, which names the first branch of a repository that git init makes.13 The release notes of Git 2.52 say that from Git 3.0 that first branch is main when nothing else is set, instead of master.14 The examples on this page set the name, so they do not depend on this default.

Pitfalls

Commits on a detached HEAD can get lost

Example detached shows the warning. Git only keeps the commit while the reflog names it. Make a branch before you switch away: git switch -c <name>.5

A remote-tracking branch is not live

origin/main changes only on git fetch, git pull or git push. When git status says "up to date", it means up to date with the last fetch. Someone may have pushed since then. Run git fetch before you trust the count.10

Force-deleting a branch removes the name of its work

git branch -D succeeds even when no other branch has the commits. The output prints the old tip, (was d54235d). Keep that name: git branch <name> d54235d brings the branch back while the object still exists.9

Comparison

Mercurial has two kinds of branch. A named branch is a field of each changeset, so the name stays in the history for good.18 A bookmark is a movable name for a changeset, close to a Git branch.19

Subversion has no branch object. By convention, a branch is a directory such as /branches/my-branch that svn copy makes from /trunk on the server. The copy takes constant time and space, because the repository points to the existing tree and does not copy data.20

Fossil records branches with tags on check-ins. A propagating branch tag applies to a check-in and its direct descendants. Fossil also names a fork: two leaves on the same branch. Autosync tries to prevent forks, and it stops a commit that would make one.21

Jujutsu calls its named pointers bookmarks and maps them to Git branches. A bookmark moves when its commit is rewritten, but there is no current bookmark, so a bookmark does not move when you make a commit.22

Perforce (P4) calls a line of development a codeline. A codeline is a set of files under a depot path, and you make one by copying another codeline with p4 integrate, p4 copy or p4 populate.23

Implementation notes

The model has not changed since 2005. The Git v0.99 tutorial makes a branch with cat .git/HEAD > .git/refs/heads/mybranch, and HEAD is a symbolic link to refs/heads/master.24 Git later made HEAD a small file that holds ref: refs/heads/..., as example pointer shows. The files backend stores each branch as a file in .git/refs/heads/, and git pack-refs can move many refs into the one file .git/packed-refs.16

Git 2.45 added a second backend, reftable, which you choose with git init --ref-format=reftable. The release notes say that it is meant to manage repositories with many refs more efficiently.17 With reftable, the ref files are only placeholders, so a script must ask Git for a ref and not read the file:

export GIT_CONFIG_GLOBAL=/dev/null GIT_CONFIG_NOSYSTEM=1
export GIT_AUTHOR_NAME=Ada GIT_AUTHOR_EMAIL=ada@example.com GIT_AUTHOR_DATE=2026-01-05T10:00:00Z
export GIT_COMMITTER_NAME=Ada GIT_COMMITTER_EMAIL=ada@example.com GIT_COMMITTER_DATE=2026-01-05T10:00:00Z
exec 2>&1
git -c init.defaultBranch=main init -q --ref-format=reftable shop && cd shop
printf 'apples\n' > list.txt
git add list.txt
git commit -q -m "Add the shopping list"
cat .git/HEAD
cat .git/refs/heads
git symbolic-ref HEAD
git rev-parse main
Output
ref: refs/heads/.invalid
this repository uses the reftable format
refs/heads/main
c6fb135760531277211142d3556246d6d0d6c46d

References

  1. Scott Chacon and Ben Straub, Pro Git, 2nd edition, Branches in a Nutshell, Branches in a Nutshell; Creating a New Branch
  2. Git documentation, gitglossary(7), branch; head
  3. Git documentation, gitglossary(7), HEAD
  4. Git documentation, git-switch(1), DESCRIPTION; OPTIONS, -c and --detach
  5. Git documentation, git-checkout(1), DETACHED HEAD
  6. Git documentation, git-reflog(1), DESCRIPTION
  7. Git documentation, git-merge-base(1), DESCRIPTION
  8. Git documentation, gitrevisions(7), SPECIFYING RANGES, Dotted Range Notations
  9. Git documentation, git-branch(1), OPTIONS, -d and -D
  10. Git documentation, gitglossary(7), upstream branch; remote-tracking branch
  11. Git documentation, git-status(1), OUTPUT, Short Format and Branch Headers
  12. Git 2.23 Release Notes, the two commands git switch and git restore
  13. Git 2.28 Release Notes, the configurable name of the first branch in new repositories
  14. Git 2.52 Release Notes, git init uses main as the initial branch, not master, starting Git 3.0
  15. Git 2.22 Release Notes, git branch learned --show-current
  16. Scott Chacon and Ben Straub, Pro Git, 2nd edition, Git Internals, Git References, Git References; The HEAD
  17. Git 2.45 Release Notes, the reftable backend, git init --ref-format=reftable
  18. Mercurial wiki (archive), NamedBranches, Named branches; Create a Branch
  19. Mercurial wiki (archive), Bookmarks, the introduction (bookmarks compared with Git branches)
  20. Version Control with Subversion, for Subversion 1.7, Chapter 4, Using Branches; Creating a Branch; Cheap Copies
  21. Fossil documentation, Branching, Forking, Merging, and Tagging, Forking Versus Branching; Tags And Properties
  22. Jujutsu documentation, Bookmarks, Introduction; Mapping to Git branches
  23. P4 CLI Documentation (formerly Helix Core), p4 integrate, Description (codelines and branching by copy)
  24. Git v0.99, Documentation/tutorial.txt, July 2005, lines 54 and 579 to 590 (HEAD as a symbolic link; a branch as a file under .git/refs/heads)

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW4 complete
  • a review of the text: no review record reviews/NJ9A34/text.yaml