Git Cheatsheet
Branches
Use this Git reference while you build software engineering projects, review code for technical interview prep, or polish examples for a software engineer resume.
Listing Branches
git branch # List local branches (* marks current) git branch -r # List remote-tracking branches git branch -a # List all branches (local + remote-tracking) git branch -v # List with last commit SHA and message git branch -vv # Also show upstream tracking info git branch --merged # List branches merged into current branch git branch --no-merged # List branches NOT yet merged git branch --merged <branch> # Branches merged into <branch> git branch --sort=-committerdate # Sort by most recently committed git branch --list "feature/*" # Filter branches by pattern
Creating Branches
git branch <name> # Create a branch at current HEAD (don't switch) git branch <name> <sha> # Create at a specific commit git branch <name> <tag> # Create at a tag git branch <name> origin/<name> # Create tracking a remote branch git checkout -b <name> # Create and switch to a new branch git checkout -b <name> <sha> # Create at a specific commit and switch git switch -c <name> # Modern syntax: create and switch git switch -c <name> <sha> # Create at specific commit and switch git switch -c <name> origin/<branch> # Create local branch tracking a remote
Switching Branches
git checkout <branch> # Switch to an existing branch git switch <branch> # Modern syntax for switching branches git switch - # Switch back to the previous branch git checkout - # Same, older syntax
Renaming Branches
git branch -m <new-name> # Rename the current branch git branch -m <old> <new> # Rename a specific branch git push origin -u <new-name> # Push renamed branch and set upstream git push origin --delete <old> # Delete the old name from remote
Deleting Branches
git branch -d <branch> # Delete (only if fully merged) git branch -D <branch> # Force delete (even if unmerged) git branch -d branch1 branch2 # Delete multiple branches # Delete a remote branch git push origin --delete <branch> git push origin :<branch> # Older syntax (same effect) # Prune stale remote-tracking refs git remote prune origin git fetch --prune # Fetch and prune at the same time
Tracking Branches (Upstream)
git branch --set-upstream-to=origin/<branch> <local> # Set upstream for existing branch git branch -u origin/<branch> # Shorthand git push -u origin <branch> # Push and set upstream in one step git branch --unset-upstream # Remove upstream tracking git branch -vv # Check tracking config for all branches
Inspecting Branches
git show-branch # Show branches and their commits git show-branch --all # Include remote-tracking branches git log <branch> # Log for a specific branch git log main..<feature> # Commits in feature not in main git diff main...<feature> # Diff from common ancestor to feature tip git merge-base main <feature> # Find common ancestor commit SHA
Orphan Branches
An orphan branch starts with no history (useful for gh-pages, docs, etc.):
git checkout --orphan <branch> # Create a branch with no parent commits git switch --orphan <branch> # Modern syntax git rm -rf . # Clear the working tree before first commit git commit --allow-empty -m "Initial commit"
Branch Workflows
Feature Branch Workflow
git switch -c feature/login # Create feature branch # ... make commits ... git switch main git merge feature/login # Merge when done git branch -d feature/login # Clean up
Checking Out a Remote Branch
git fetch origin # Make sure remote refs are up to date git switch -c <branch> origin/<branch> # Create local branch from remote # or shortcut (auto-tracks if name matches): git checkout <branch>
Tips and Gotchas
A branch in Git is just a pointer (file in
.git/refs/heads/) to a commit SHA. Switching branches is nearly instant regardless of repo size.
git switchis the modern replacement forgit checkoutfor branch operations.git checkoutstill works but is overloaded (files vs. branches).
Deleting a local branch does NOT delete it on the remote. Use
git push origin --delete <branch>.