Glossary

Git Branch

A branch in git is a lightweight, movable pointer to a commit. Creating a branch takes milliseconds and costs almost no disk space—far cheaper than in older VCS where branches were heavy copies. This is why git encourages frequent branching: for every feature, every bug fix, every experiment.

git branch                          # list local branches
git branch -a                        # include remotes
git branch feature                   # create a branch
git switch feature                   # switch to it (modern)
git checkout feature                 # switch (older syntax)
git switch -c feature                # create and switch
git merge feature                    # merge into current
git branch -d feature                # delete merged branch
git branch -D feature                # force delete

The default branch is traditionally called main (formerly master by historical convention). The current branch is shown by HEAD, which is a reference to a branch, which in turn points to a commit—this double indirection is what makes git commit update the branch to point at the new commit automatically.

Branches can be pushed to remotes, at which point they appear as origin/feature or similar. The distinction between local branches and remote-tracking branches is one of the subtleties that trip up new users, but it is the foundation of git's collaboration model.

Related terms: Git, Git Commit, Git Merge, checkout

Discussed in:

Also defined in: Textbook of Linux