Git Cheat Sheet
Complete Git cheat sheet with commands for initialization, branching, remotes, stashing, undoing mistakes, tags, and configuration.
Git basics
Start repositories, inspect history, and publish your work.
Initialize and clone
git init # Initialize a new local Git repository git clone <repo-url> # Download repository from remote URL git clone <repo-url> <directory> # Clone repository into a specific directory
Status and history
git status # Show repository working directory and staging status git status --short --branch # Show compact status with current branch info git log # View commit history git log --oneline --graph --decorate --all # View graphical history across all branches on single lines git log --stat # View history with file change statistics
Add and commit
git add <file> # Stage specific file for commit git add . # Stage all modified and new files git add -p # Interactively select chunks of changes to stage git commit -m "message" # Commit staged changes with a descriptive message git commit --amend --no-edit # Add staged changes to latest commit without changing message
Working tree and staging
Commands to inspect and restore file state safely.
View changes
git diff # Show unstaged modifications in working directory git diff --staged # Show modifications in staging area ready to commit git diff <commit> <file> # Show differences in a file relative to a specific commit git diff HEAD~1..HEAD # Show differences between previous and latest commit
Restore files
git restore <file> # Discard local uncommitted changes in working directory git restore --staged <file> # Unstage file changes while keeping local modifications git checkout -- <file> # Older command to discard local modifications
Remove files
git rm <file> # Remove file from working directory and staging area git rm --cached <file> # Remove file from tracking while keeping local file on disk
Branching
Create, switch, and maintain branches.
Branch commands
git branch # List local branches git branch <name> # Create new branch git branch -d <name> # Delete branch if fully merged git branch -D <name> # Force delete branch regardless of merge status
Switch branches
git switch <branch> # Switch to target branch git switch -c <branch> # Create and switch to new branch git checkout <branch> # Older command to switch branches git switch -C <branch> # Force create or reset and switch to target branch
Branch tracking
Connect local branches to remotes and rename branches.
Tracking branches
git branch -u origin/<branch> # Link current local branch to remote upstream branch git push -u origin <branch> # Push local branch to remote and configure tracking git branch -vv # List local branches with upstream information and commit details
Rename branch
git branch -m <old> <new> # Rename local branch git push origin --delete <old> # Remove old branch from remote git push origin -u <new> # Push new branch name and set upstream
Merge and rebase
Integrate changes from one branch to another cleanly.
Merge
git checkout main # Switch to target branch for merge git merge feature-branch # Merge feature branch into current branch git merge --no-ff feature-branch# Merge and force creation of a merge commit git merge --abort # Abort active merge process and restore state
Rebase
git checkout feature-branch # Switch to branch to rebase git rebase main # Reapply commits from current branch on top of main # Interactive rebase git rebase -i HEAD~<n> # Reorder, squash, or edit last n commits interactively git rebase --continue # Resume rebase process after resolving conflicts git rebase --skip # Skip current conflicting patch during rebase git rebase --abort # Abort rebase operation and restore original state
Remote repositories
Fetch, share, and keep your local repository in sync.
Remote setup
git remote -v # List configured remote repositories with URLs git remote add origin <url> # Attach local repo to a remote repository URL git remote remove origin # Disconnect remote repository handle git remote show origin # Inspect detailed status of remote repository
Fetch, pull, push
git fetch origin # Download history and references from remote without merging git pull origin <branch> # Fetch and merge remote changes into active branch git pull --rebase origin <branch> # Fetch and reapply local commits on top of incoming remote branch git push origin <branch> # Upload local branch commits to remote repository git push origin --delete <branch> # Remove specified branch from remote repository
Remote branch workflows
Common commands for branch sharing and cleanup.
Publish and track
git push -u origin <branch> # Push new branch to remote and set default tracking git pull --rebase # Rebase current branch on tracked upstream changes git push --force-with-lease # Safely overwrite remote branch history if unchanged by others
Clean up stale remotes
git remote prune origin # Remove local tracking references to deleted remote branches git fetch --prune # Fetch updates and purge deleted remote branch references git branch -r # List all branches on remote repositories
Stash
Temporarily store work in progress and reapply it later.
Save and restore
git stash save "message" # Save uncommitted changes with description (deprecated style)
git stash push -m "message" # Save uncommitted work-in-progress changes with a label
git stash list # Display all saved stashes
git stash show -p stash@{0} # Inspect diff contents of a specific stash
git stash apply stash@{0} # Reapply stash changes while retaining stash entry
git stash pop # Apply most recent stash and remove it from stack
git stash drop stash@{0} # Delete specified stash from stack
git stash clear # Remove all saved stashes from stackBranch from stash
git stash branch <name> stash@{0} # Create new branch and apply specified stash to itUndo and recover
Fix mistakes without losing work.
Commit history
git commit --amend # Modify latest commit message or add newly staged changes git revert <commit> # Create new commit that undoes changes from a target commit git cherry-pick <commit> # Apply changes introduced by an existing commit to current branch
Reset modes
git reset --soft HEAD~1 # Undo commit, leave changes staged in index git reset --mixed HEAD~1 # Undo commit, leave changes in working directory (unstaged) git reset --hard HEAD~1 # Undo commit and permanently delete all local modifications
Recover lost commits
git reflog # View chronological log of reference HEAD position changes git checkout <sha> # Switch working directory state to isolated commit SHA git switch -c recovery <sha> # Create new branch starting from recovered commit SHA
Bisect
Binary search to locate the commit introducing a bug.
Bisect flow
git bisect start # Begin binary search session for bug hunting git bisect bad # Mark current commit as containing the bug git bisect good <commit> # Mark target historical commit as bug-free # test, then mark each step git bisect good # Mark current step commit as bug-free # or git bisect bad # Mark current step commit as containing bug git bisect reset # End bisect session and return to original HEAD
Tags
Annotate releases and snapshots in history.
Create tags
git tag <name> # Create lightweight pointer tag at HEAD git tag -a <name> -m "message" # Create annotated tag storing metadata and message git tag -s <name> -m "message" # Create GPG-signed annotated tag
Push tags
git push origin <tag> # Send specified tag to remote repository git push --tags # Send all local tags to remote repository git push --follow-tags # Send annotated tags associated with pushed commits
Configuration
Set user identity, defaults, and aliases.
User settings
git config --global user.name "Your Name" # Set author name for global commits git config --global user.email "you@example.com" # Set author email for global commits git config --global core.editor "code --wait" # Set VS Code as default Git commit editor
Useful config
git config --global core.autocrlf input # Normalize line endings to LF on commit git config --global pull.rebase true # Make rebase default behavior for git pull git config --global push.default current # Restrict git push default to active branch git config --global alias.co checkout # Set 'co' shortcut for checkout git config --global alias.br branch # Set 'br' shortcut for branch git config --global alias.ci commit # Set 'ci' shortcut for commit git config --global alias.st status # Set 'st' shortcut for status git config --global alias.lg "log --oneline --graph --decorate --all" # Set custom formatted log alias
Workflow patterns
Common branching models and daily work routines.
Feature branch workflow
Create a branch for each feature, rebase or merge into main, then delete the branch.
Trunk-based workflow
Commit small, frequent changes to main and use short-lived branches for fixes or experiments.
Git flow essentials
Use enduring main and develop branches plus release, hotfix, and feature branches. Keep merges intentional.
Advanced references
Commands for history inspection, patch workflows, and efficiency.
Inspect history
git show <commit> # Display commit details and file content changes git show <branch>..<branch> # View differences between two branch endpoints git blame <file> # Display file line-by-line with author and commit metadata git diff --name-only HEAD~1 # List names of files changed in previous commit
Save work in progress
git stash push -k -m "wip" # Stash unstaged modifications while preserving staged items git stash push --include-untracked # Stash uncommitted changes including newly untracked files
Cleanup
git gc --aggressive --prune=now # Optimize repository storage and delete unreachable objects git stash clear # Purge all entries from local stash store