Branching
List all branches that are already merged into master
git branch --merged master
Remove branches that have already been merged with master
git branch --merged master | grep -v '^\*' | xargs -n 1 git branch -d
Alternatives:
git branch --merged master | grep -v '^\*\| master' | xargs -n 1 git branch -d # will not delete master if master is not checked out
List all branches and their upstreams, as well as last commit on branch
git branch -vv
Track upstream branch
git branch -u origin/mybranch
Delete local branch
git branch -d <local_branchname>
Get list of all local and remote branches
git branch -a
Get only remote branches
git branch -r
Find out branches containing commit-hash
git branch -a --contains <commit-ish>
Alternatives:
git branch --contains <commit-ish>
Rename a branch
git branch -m <new-branch-name>
Alternatives:
git branch -m [<old-branch-name>] <new-branch-name>
Archive the master branch
git archive master --format=zip --output=master.zip
Delete local branches that has been squash and merged in the remote.
git branch -vv | grep ': gone]' | awk '{print $1}' | xargs git branch -D
Export a branch with history to a file.
git bundle create <file> <branch-name>
Get the name of current branch.
git rev-parse --abbrev-ref HEAD
Show the most recent tag on the current branch.
git describe --tags --abbrev=0
List all branch is WIP
git checkout master && git branch --no-merged
Preformatted patch file.
git format-patch -M upstream..topic
Switch to a branch (modern alternative to checkout)
git switch <branch-name>
Alternatives:
git switch -c <new-branch-name>