Skip to main content

Git Cheatsheet

Git Helpers

Interactive, searchable reference of essential git commands organized by category with syntax, descriptions, and examples.

Setup
git config --global user.name "Name"
Set your global username
git config --global user.name "Jane Doe"
git config --global user.email "email"
Set your global email
git config --global user.email "jane@example.com"
git config --list
Show all git configuration settings
git config --list
git init
Initialize a new git repository
git init my-project
git clone <url>
Clone a remote repository
git clone https://github.com/user/repo.git
Staging
git status
Show working tree status
git status
git add <file>
Stage a specific file
git add index.html
git add .
Stage all changes in current directory
git add .
git add -p
Interactively stage chunks of changes
git add -p
git reset HEAD <file>
Unstage a file (keep changes)
git reset HEAD style.css
git rm --cached <file>
Remove file from staging (keep on disk)
git rm --cached secrets.env
Branching
git branch
List all local branches
git branch
git branch <name>
Create a new branch
git branch feature/login
git branch -d <name>
Delete a branch (safe)
git branch -d old-feature
git branch -D <name>
Force delete a branch
git branch -D broken-feature
git branch -a
List all branches (local + remote)
git branch -a
git checkout <branch>
Switch to an existing branch
git checkout main
git checkout -b <branch>
Create and switch to new branch
git checkout -b feature/auth
git switch <branch>
Switch branches (modern syntax)
git switch develop
git switch -c <branch>
Create and switch (modern syntax)
git switch -c feature/api
Merging
git merge <branch>
Merge a branch into current branch
git merge feature/login
git merge --no-ff <branch>
Merge with a merge commit (no fast-forward)
git merge --no-ff feature/auth
git merge --squash <branch>
Squash all commits into one before merging
git merge --squash feature/cleanup
git merge --abort
Abort a merge in progress
git merge --abort
Stashing
git stash
Stash working directory changes
git stash
git stash push -m "msg"
Stash with a descriptive message
git stash push -m "WIP: login form"
git stash list
List all stashes
git stash list
git stash pop
Apply most recent stash and remove it
git stash pop
git stash apply stash@{n}
Apply a specific stash (keep it)
git stash apply stash@{2}
git stash drop stash@{n}
Delete a specific stash
git stash drop stash@{0}
git stash clear
Delete all stashes
git stash clear
Remote
git remote -v
List remote repositories
git remote -v
git remote add <name> <url>
Add a remote repository
git remote add origin https://github.com/user/repo.git
git fetch
Download objects/refs from remote
git fetch origin
git pull
Fetch and merge remote changes
git pull origin main
git push
Push commits to remote
git push origin main
git push -u origin <branch>
Push and set upstream branch
git push -u origin feature/auth
git push --force-with-lease
Force push safely (checks remote state)
git push --force-with-lease
Undoing
git checkout -- <file>
Discard changes in a file
git checkout -- index.html
git restore <file>
Discard changes (modern syntax)
git restore style.css
git reset --soft HEAD~1
Undo last commit, keep changes staged
git reset --soft HEAD~1
git reset --mixed HEAD~1
Undo last commit, unstage changes
git reset --mixed HEAD~1
git reset --hard HEAD~1
Undo last commit, discard changes
git reset --hard HEAD~1
git revert <commit>
Create a new commit that undoes a previous one
git revert a1b2c3d
git clean -fd
Remove untracked files and directories
git clean -fd
Viewing History
git log
Show commit history
git log
git log --oneline
Compact commit history (one line each)
git log --oneline
git log --oneline --graph --all
Visual branch graph
git log --oneline --graph --all
git log -n <num>
Show last N commits
git log -n 5
git log --author="name"
Filter commits by author
git log --author="Jane"
git log -- <file>
Show commits that modified a file
git log -- src/app.js
git show <commit>
Show details of a specific commit
git show a1b2c3d
git diff
Show unstaged changes
git diff
git diff --staged
Show staged changes
git diff --staged
git blame <file>
Show who last modified each line
git blame src/utils.js
Tags
git tag
List all tags
git tag
git tag <name>
Create a lightweight tag
git tag v1.0.0
git tag -a <name> -m "msg"
Create an annotated tag
git tag -a v1.0.0 -m "Release 1.0.0"
git push origin <tag>
Push a tag to remote
git push origin v1.0.0
git push origin --tags
Push all tags to remote
git push origin --tags
git tag -d <name>
Delete a local tag
git tag -d v0.9.0
Advanced
git rebase <branch>
Reapply commits on top of another branch
git rebase main
git rebase -i HEAD~n
Interactive rebase for last N commits
git rebase -i HEAD~3
git cherry-pick <commit>
Apply a specific commit to current branch
git cherry-pick a1b2c3d
git bisect start
Start binary search for a buggy commit
git bisect start
git reflog
Show history of HEAD movements
git reflog
git shortlog -sn
Commit count by author (leaderboard)
git shortlog -sn --all
git archive --format=zip HEAD -o out.zip
Create a zip archive of the repo
git archive --format=zip HEAD -o project.zip
git submodule add <url>
Add a submodule
git submodule add https://github.com/lib/lib.git libs/lib
git worktree add <path> <branch>
Create a linked working tree
git worktree add ../hotfix-tree hotfix/urgent