Version control
What is a Git Branch?
Definition
A branch is a movable pointer to a commit, letting one line of work proceed separately from another until the two are merged back together.
Also known as: Git branch
Branch in more detail
In Git a branch is astonishingly light: a file under .git/refs/heads containing a single commit hash. Creating one writes forty-one bytes; there is no copying of files and no server involved. When you commit, the branch pointer moves forward to the new commit. HEAD is a pointer to the branch you currently have checked out, which is why 'detached HEAD' simply means HEAD points at a commit directly instead of at a branch.
Branches exist so that unfinished work does not disturb the line everyone else builds on. The dominant convention is one long-lived branch called main plus short-lived branches for individual pieces of work, each opened as a pull request and deleted after merging. Older Git Flow adds develop, release and hotfix branches, which suits versioned software with scheduled releases and is heavy for anything shipping continuously.
The practical warning is that branches diverge as they age. A branch alive for three weeks accumulates conflicts against a main branch that has moved on, and the merge becomes the risky part of the work. Trunk-based development pushes the opposite way — very short-lived branches, merged daily, with unfinished behaviour hidden behind feature flags rather than kept on a branch. Note also that a branch and a fork differ: a fork is a separate copy of the whole repository, typically used when contributors lack write access.
Branch example
$ cat .git/refs/heads/main
9f2c1b7a0e4d3c5b6a8f... # that is the entire branch
$ git switch -c feature/search # create and switch
$ git commit -m "Add index" # the pointer moves forward
$ git switch main # main never saw those commits
$ git merge feature/search # now it does
$ git branch -d feature/search # pointer deleted, commits kept
main A---B---C-----------M
\ /
feature D---E---+
Keep branches short. A three-week-old branch is not a branch,
it is a merge waiting to happen.Branch — common questions
- What is the difference between a branch and a fork?
- A branch is another line of development inside one repository. A fork is a full copy of the repository under a different owner, used mainly when the contributor cannot push to the original — the open-source contribution model.
- Does creating a branch copy the files?
- No. Git writes one small file containing a commit hash, so branching is effectively instantaneous regardless of project size. Switching branches updates your working tree, but the branch itself is just a pointer.
- What does 'detached HEAD' mean?
- You have checked out a commit directly rather than a branch, so HEAD points at a commit instead of a branch name. New commits made there belong to no branch and can be lost — create a branch to keep them.
Keep reading
Every definition in the set is indexed A-Z in the developer glossary, and the cheat sheets cover the syntax once you know what you are looking for.
Browse the A-Z glossary