Git branch

The git branch command allows you to create, list, rename, and delete branches. A branch is an independent line of development in your Git repository, which makes it easy to work on different features or fixes simultaneously.



Tutorials dojo strip

Initial Example

To list all branches in your repository, use:

git branch




BRANCH Options

OptionDescription
-d, --deleteDelete a branch. The branch must be fully merged in its upstream branch or in HEAD.
-DForce delete a branch, even if it is not fully merged.
--create-reflogCreate the branch’s reflog to record all changes.
-f, --forceForcefully reset a branch or delete/rename a branch, even if it exists or is not valid.
-m, --moveRename or move a branch, including its config and reflog.
-MForce rename or move a branch, even if the new name already exists.
-c, --copyCopy a branch, including its config and reflog.
-CForce copy a branch, even if the new name already exists.
--color[=<when>]Color branches to highlight current, local, and remote-tracking branches.
--no-colorTurn off branch colors.
-i, --ignore-caseMake sorting and filtering of branches case insensitive.
--omit-emptyDo not print a newline after formatted refs if the format expands to an empty string.
--column[=<options>], --no-columnDisplay branch listing in columns, based on configuration variable column.branch.
-r, --remotesList or delete remote-tracking branches.
-a, --allList both remote-tracking branches and local branches.
-l, --listList branches that match the optional pattern(s).
--show-currentPrint the name of the current branch. Nothing is printed in detached HEAD state.
-v, -vv, --verboseShow SHA-1 and commit subject line for each head. Additional details when used twice.
-q, --quietSuppress non-error messages when creating or deleting a branch.
--abbrev=<n>Show at least hexadecimal digits of the SHA-1 in verbose mode.
--no-abbrevDisplay the full SHA-1 in the output listing.
-t, --track[=(direct|inherit)]Set up branch tracking configuration for the new branch.
--no-trackDo not set up upstream tracking configuration.
--recurse-submodulesRecurse into submodules if submodule.propagateBranches is enabled.
-u <upstream>, --set-upstream-toSet up tracking information for the branch to track the specified upstream branch.
--unset-upstreamRemove the upstream tracking information for the branch.
--edit-descriptionOpen an editor to edit the description for the branch.
--contains [<commit>]List branches which contain the specified commit.
--no-contains [<commit>]List branches which do not contain the specified commit.
--merged [<commit>]List branches whose tips are reachable from the specified commit.
--no-merged [<commit>]List branches whose tips are not reachable from the specified commit.
<branchname>The name of the branch to create, delete, move, or copy.
<start-point>The commit that the new branch head will point to. Defaults to the current HEAD if omitted.
<oldbranch>The name of an existing branch to delete or rename. Defaults to the current branch if omitted.
<newbranch>The new name for an existing branch.
--sort=<key>Sort branches based on the specified key.
--points-at <object>List branches pointing to the given object.
--format <format>Format the output using the specified string interpolation.




Examples

1. Listing All Branches

To see a list of all branches in your repository:

git branch

This will show all branches and highlight the current branch with an asterisk (*):

* main
  feature-branch

2. Creating a New Branch

To create a new branch named feature-branch, use:

git branch feature-branch

This creates a new branch, but does not switch to it. To switch to the new branch, use:

git checkout feature-branch

Or, you can create and switch to the new branch in one step:

git checkout -b feature-branch

3. Deleting a Branch

To delete a branch named feature-branch, use:

git branch -d feature-branch

If the branch has not been fully merged, you can force deletion with:

git branch -D feature-branch

Scroll to Top