Skip to main content

Branch

Updated 2 min read

Share this page

Send the link, quote the definition with a link back, or show it as a card on your own site.

https://softwaredictionary.org/terms/branch

In short

A branch in Git is an independent line of development that lets you work on a feature or fix in isolation, without affecting the main code until you merge it.

What is a branch in Git?

A branch lets you split off from the main line of work to build something new, such as a feature, a bug fix, or an experiment. Commits you make on a branch don't affect other branches, so the main code stays stable while you work. When the work is ready, you combine it back, usually through a merge or a pull request.

In Git, a branch is surprisingly lightweight: it is just a movable pointer to a commit. Each time you commit on a branch, the pointer moves forward to the new commit. A special pointer called HEAD tracks which branch you are currently on, which is why creating or switching branches takes almost no time.

A branch is like a parallel copy of a document where you can try out edits freely. If the edits turn out well, you fold them into the original; if not, you simply throw the copy away. Most repositories have a default branch, commonly called main (older projects often use master), which holds the official version of the code.

Teams usually follow a branching strategy. In trunk-based development, developers create short-lived branches and merge them into main frequently, often several times a day, while heavier workflows like Git Flow keep long-lived branches for development, releases, and urgent fixes. A branch is also different from a fork, which is a complete copy of a repository, usually under another account.

At a glance

A feature branch splits off main after commit B, gets commits D and E, and is merged back into main as commit F.mergeABCFDEmainHEADfeature/login
A branch is just a movable label on a commit. Work on it doesn't touch main until you merge.

Key takeaways

  • A branch is an isolated line of development within a repository.
  • In Git, a branch is just a lightweight pointer to a commit.
  • The default branch is usually called main.
  • Short-lived branches that merge often reduce merge conflicts.

Example

Working on a feature branchbash
# Create a new branch and switch to it
git switch -c feature/search-bar

# Edit files, then commit on the branch
git add .
git commit -m "Add search bar component"

# Switch back to main and merge the feature
git switch main
git merge feature/search-bar

# Delete the branch once it has been merged
git branch -d feature/search-bar

Readers ask

What is the difference between git switch and git checkout?

git switch is a newer command, added in Git 2.23, that does only one job: changing branches. git checkout can also switch branches but handles other tasks too, such as restoring files, which many beginners find confusing.

What is the difference between a branch and a fork?

A branch is a separate line of work inside the same repository. A fork is a full copy of a repository, typically under a different account, often used to contribute to projects you don't have write access to.

Why is the default branch called main instead of master?

Around 2020, major Git hosting platforms and many projects changed their default branch name from master to main to use more inclusive language. Git itself lets you pick any default name with the init.defaultBranch setting.

Often compared

See also

Sources

Spotted a mistake or something missing on this page?Suggest an edit

Read a random page
Open today's review
Switch to the dark theme
Read this page in Türkçe

More

Settings