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.
Initial Example
To list all branches in your repository, use:
git branch
BRANCH Options
Option | Description |
---|---|
-d, --delete | Delete a branch. The branch must be fully merged in its upstream branch or in HEAD. |
-D | Force delete a branch, even if it is not fully merged. |
--create-reflog | Create the branch’s reflog to record all changes. |
-f, --force | Forcefully reset a branch or delete/rename a branch, even if it exists or is not valid. |
-m, --move | Rename or move a branch, including its config and reflog. |
-M | Force rename or move a branch, even if the new name already exists. |
-c, --copy | Copy a branch, including its config and reflog. |
-C | Force copy a branch, even if the new name already exists. |
--color[=<when>] | Color branches to highlight current, local, and remote-tracking branches. |
--no-color | Turn off branch colors. |
-i, --ignore-case | Make sorting and filtering of branches case insensitive. |
--omit-empty | Do not print a newline after formatted refs if the format expands to an empty string. |
--column[=<options>], --no-column | Display branch listing in columns, based on configuration variable column.branch. |
-r, --remotes | List or delete remote-tracking branches. |
-a, --all | List both remote-tracking branches and local branches. |
-l, --list | List branches that match the optional pattern(s). |
--show-current | Print the name of the current branch. Nothing is printed in detached HEAD state. |
-v, -vv, --verbose | Show SHA-1 and commit subject line for each head. Additional details when used twice. |
-q, --quiet | Suppress non-error messages when creating or deleting a branch. |
--abbrev=<n> | Show at least hexadecimal digits of the SHA-1 in verbose mode. |
--no-abbrev | Display the full SHA-1 in the output listing. |
-t, --track[=(direct|inherit)] | Set up branch tracking configuration for the new branch. |
--no-track | Do not set up upstream tracking configuration. |
--recurse-submodules | Recurse into submodules if submodule.propagateBranches is enabled. |
-u <upstream>, --set-upstream-to | Set up tracking information for the branch to track the specified upstream branch. |
--unset-upstream | Remove the upstream tracking information for the branch. |
--edit-description | Open 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