Branches
CREATE
Create a branch
git branch <branch-name>
Create a branch and switch to it
common
git checkout -b <branch-name>
new
git switch -c <branch-name>
If there is a remote branch, but you need to create a local one and link to the remote one
git branch --track <local-branch> origin/<remote-branch>
git branch <local-branch> origin/<remote-branch>
git checkout -b <local-branch> origin/<remote-branch>
If there is already a local one with the same name, then it will link to the remote one
git branch -f <local-branch> origin/<remote-branch>
If there is a local branch and you need to create a remote one with tracking
git push -u origin <branch-name>
If there are both local and remote branches and you need to make tracking
git branch -u origin/<remote-branch-name> <local-branch-name>
If you need to remove tracking
git branch --unset-upstream <branch-name>
MOVE
Move branch pointer to a different commit (destination-commit can be a branch name)
git reset --hard <destination-commit>
git branch –f <branch-name> <destination-commit>
DELETE
DELETE remote branch (remote-name usually origin)
git push -d <remote-name> <branch-name>
Don’t forget to do a git fetch --all --prune or git fetch --all -p on other machines after deleting the remote branch on the server.
After deleting the local branch with git branch -d and deleting the remote branch with git push -d origin other machines may still have “obsolete tracking branches” (to see them do git branch -a). To get rid of these do git fetch --all --prune
DELETE local branch
git branch -d <branch-name>
The -D option is an alias for –delete –force, which deletes the branch “irrespective of its merged status”
git branch -D <branch-name>
DELETE local remote-tracking branch
git branch -dr <remote>/<branch>
long
git branch --delete --remotes <remote>/<branch>
RENAME
Rename current branch
git branch -m <branch_name>
Rename another branch
git branch -m <old_branch_name> <new_branch_name>
Rename branch on the remote
Push the local branch and reset the upstream branch
git push origin -u <new_branch_name>
Delete the remote branch
git push origin --delete <old_branch_name>
VIEW
View branches that was merged with the current branch
git branch --merged
View remote branches that was merged with the current branch
git branch -r --merged
View branches that was’t merged with the current branch
git branch --no-merged
PRUNE
git remote prune origin
short
git fetch -p
long
git fetch --prune