This page is not available in every language. Missing translations are disabled in the language selector.

Quick reference

Git Branching Cheatsheet

A quick reference guide for Git branch management: creating, switching, upstream tracking, listing, merged branch cleanup, and force deletion.

· Published

Branching is Git's lightweight, pointer-based mechanism for isolating parallel streams of development.

Working with Branches

Creating & Tracking

git checkout -b $branchname
git push origin $branchname --set-upstream

Creates a new branch locally and immediately establishes upstream tracking on the remote repository.

Tracking Existing Remote Branch

git fetch origin
git checkout --track origin/$branchname

Fetches the remote branch and creates a local working branch configured to track it.

Pruning Stale Tracking References

git remote prune origin

Deletes local tracking references (origin/*) whose upstream branches have been deleted on the remote.

Listing Branches

git branch --list       # Local branches
git branch -a           # All branches (local & remote)
git branch -r           # Remote tracking branches only

Lists existing branches; the active checked-out branch is marked with an asterisk (*).

Listing Merged Branches

git branch -a --merged
git branch --no-merged

Identifies branches that have already been integrated into HEAD versus branches containing unmerged work.

Deleting Branches

Safe Local Deletion

git branch -d $branchname

Deletes a local branch only if its commits have been fully merged upstream into the active branch.

Forcible Local Deletion

git branch -D $branchname

Note: -D is a shortcut for --delete --force. It forcibly destroys the branch reference regardless of merge status.

Deleting Remote Branch

git push origin --delete $branchname
# or older syntax:
git push origin :$branchname

Deletes the branch directly on the remote server. Also works for remote tags (git push origin --delete tag_name).

Retrieving Active Commit Hash

git show-ref HEAD -s
git rev-parse HEAD

Discarding Uncommitted Working Changes

git reset --hard HEAD

Rolling Back to a Specific Commit

git reset --hard $commit_id

# Push safely to remote (aborts if someone else pushed since your last fetch)
git push --force-with-lease

# Or push unconditionally
git push --force

References

Related topics