Git branch
Internal
Overview
git branch is used to perform branch-related operations: listing existing branches, creating new local and remote branches, renaming branches and deleting branches.
Use git checkout to check out the content of a branch in the local work area.
For a description of the concepts behind Git branches, see:
List Existing Branches
The local (topic) branches in the repository are displayed by executing git branch without any argument, or with the default --list argument:
git branch [--list]
The output of the command reflects the content of the .git/refs/heads/ directory.
The tracking branches are displayed by executing:
git branch -r
The output of the command reflects the content of the .git/refs/remotes/<remote-name> directories.
To display both local and tracking branches, use:
git branch -a
Branch Details
git show-branch
Create a New Local Branch
A new local branch can be created with
git branch <new-branch-name>
This creates a new local topic branch new-branch-name rooted in the HEAD of the current branch, so the default behavior is to create a branch right at the point where you're working right now. An alternate starting commit can be provided by specifying the commit hash.
git branch <new-branch-name> [starting-commit]
Immediately after creation, the branch exists just as a name in the local repository. It does not change the working directory to use the new branch. From an implementation point of view, the only effect is that a new refs/heads/<new-branch-name>> ref appears. If you want to use the newly created branch, you need to check it out (switch to it) with git checkout.
The newly created branch is not shared by default with any remote repository.
What Happens if There Are Work Tree Changes at the Time of New Branch Creation?
If you start modifying local files and then decide to create a new branch for those modifications, you can simply go ahead and create the branch, check out the new branch and commit the changes on the new branch - the changes will be transferred to the new branch without any complications.
Publish a Local Branch in a Remote Repository
{{Note: This sections was written with the assumption that the value of 'push.default' is "simple". This is the recommended value, which leads to a more deterministic behavior. For more details see Git Configuration Options#push.default.}}