git branch
List, create, rename or delete branches
What it does
A branch is a movable label on a commit: you work on a separate task in it without touching the main branch. With no arguments git branch lists local branches and marks the current one with an asterisk. With a name it creates a new branch at the current commit but does not switch to it.
-m renames a branch, -d deletes one. -d refuses to delete a branch that has not been merged, which protects your work. -D deletes without checking.
Careful. git branch -D deletes a branch even if its commits exist nowhere else. After that, only git reflog can bring them back.
Syntax
git branch
git branch <name>
git branch -m <old-name> <new-name>
git branch -d <name>Examples
List local branches
git branchbranch— branches; * marks the current one
Create the branch feature/cart without switching to it
git branch feature/cartbranch— create a branchfeature/cart— new branch name
Rename the branch feature/cart to feature/basket
git branch -m feature/cart feature/basketsame as:
git branch --move feature/cart feature/basket-m— move — renamefeature/cart feature/basket— old name → new name
Delete the already merged branch feature/login
git branch -d feature/loginsame as:
git branch --delete feature/login-d— delete, only if the branch is mergedfeature/login— branch
Related commands
- git switch — Switch to another branch, or create a new one and switch to it
- git merge — Bring the changes from another branch into the current one
- git push — Send your commits to the server