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
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
# 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-barReaders 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
- GitVersion Control, p. 10Git is a free, open-source distributed version control system that tracks changes to files over time, so developers can collaborate and undo mistakes.
- CommitVersion Control, p. 5A commit is a saved snapshot of a project's files in Git, recorded with a unique ID, an author, a timestamp, and a message describing what changed.
- MergeVersion Control, p. 29A merge in Git combines the changes from one branch into another, joining separate lines of development back together into a single, shared history.
- RebaseVersion Control, p. 33A rebase in Git replays a branch's commits on top of another commit, rewriting history to produce a cleaner, linear sequence of changes without merge commits.
- Pull RequestVersion Control, p. 32A pull request is a proposal to merge changes from one branch into another, giving teammates a place to review, discuss, and test the code before it is merged.
- ForkVersion Control, p. 9A fork is a personal copy of someone else's repository on a hosting platform, letting you experiment freely and propose changes back to the original project.
- Git CheckoutVersion Control, p. 12git checkout switches to another branch or commit, or restores files to an earlier version; newer Git splits these jobs into git switch and git restore.
Sources
Spotted a mistake or something missing on this page?Suggest an edit