Git Branches | git branch
The git branch command
A branch is an independent line of development. Branches are an abstraction built on top of the edit, stage, and commit process explained in the Git basics chapter. You can think of them as a way to create separate histories from the same working directory and staging area. New commits are recorded in the history of the current branch, forming a branch in the project history.
git branch creates, lists, renames, and deletes branches. It does not switch branches, merge branched histories, or undo changes. For that reason, git branch is often used together with git checkout and git merge.
Usage
git branch
Lists all branches in the repository.
git branch <branch>
Creates a new branch named <branch>. The newly created branch is not checked out automatically.
git branch -d <branch>
Deletes the specified branch. This is a safe operation because Git refuses to delete the branch if it contains unmerged changes.
git branch -D <branch>
Forces deletion of the specified branch, even if it contains unmerged changes. Use this when you intentionally want to delete all commits created on a specific line of development.
git branch -m <branch>
Renames the current branch to <branch>.
Additional notes
Branches are part of everyday development in Git. When you develop a new feature or fix a bug, regardless of size, you create a branch to encapsulate the change. This prevents unstable code from being committed directly to the main codebase and lets you clean up the feature history before merging it into master.
Git Tutorial: git branch
For example, a repository can have two independent development lines: one for a small feature and another for a long-running feature. Branching these efforts lets both proceed at the same time while protecting the main master branch from incomplete code.
Branch tips
The design behind Git branches is much lighter than branching in SVN. SVN copies files directly between directories, while Git stores branches as references to commits. In that sense, a branch is not a container of commits. It is a pointer to the tip of a line of work. The history of a branch is inferred from the relationships between commits.
This has a major impact on Git’s merge model. SVN merging is file-based, while Git works at the abstraction level of commits. In practice, merging project history means combining independent commit histories.
Examples
Creating a branch
It is important to understand that a branch is just a pointer to a commit. Creating a branch only creates a pointer; it does not change the repository. Suppose the repository currently has a history like the one shown in the tutorial diagram.
Create a branch with the following command:
git branch crazy-experiment
This does not change the repository history. The only new object is a pointer to the current commit.
Git Tutorial: Creating a new branch
Remember that this operation only creates a new branch. To start adding commits to that branch, select it with git checkout, then use the normal git add and git commit commands. See the git checkout section in this chapter for details.
Deleting a branch
After work on a branch is finished and merged into master, you can delete the branch without losing history:
git branch -d crazy-experiment
If the specified branch has not been fully merged, the command displays an error:
error: The branch 'crazy-experiment' is not fully merged.
If you are sure you want to delete it, run 'git branch -D crazy-experiment'.
This prevents losing the only reference to those commits and accidentally losing access to an entire line of development. If you are sure that the branch can be deleted, for example because the experiment failed, use the uppercase -D flag:
git branch -D crazy-experiment
This command deletes the branch without warning regardless of its state, so use it carefully.
Types of Git branches
Master branch
- A branch that can be released as a product.
- Used to manage release history. In other words, it only tracks releasable states.
Develop branch
- A branch for developing the next release.
- Used to merge feature branches. When all features have been added, bugs have been fixed, and the branch is stable enough to release, the
developbranch is merged intomaster. - Day-to-day development is usually based on this branch.
Feature branch
- A branch for developing a feature.
- A feature branch is created from
developwhenever a new feature or bug fix is needed. Work on a feature branch usually does not need to be shared, so it can be managed in the developer’s local repository. - When development is complete, it is merged into
developand shared with others.
Release branch
- A branch for preparing the current release.
- Using a dedicated release branch lets one team prepare the release while another team continues developing features for the next release. It is useful for defining clear development stages.
- For example, it makes it easier for the team to agree on a goal such as “release version 1.3 this week.”
Hotfix branch
- A branch for fixing bugs in a released version.
- When an urgent fix is needed for a released version, this branch is created from
master. Fixing the problem ondevelopand preparing a releasable version may take too long and may not guarantee stability, so the fix is made directly from the releasablemasterbranch, merged back intomaster, and then released.