Git Cheat Sheet

Essential commands for version control, branching, and collaboration

Setup & Configuration
git config --global user.name "Name"
Set your global git username
git config --global user.email "[email protected]"
Set your global git email
git config --list
Show all git configuration settings
git config user.name
Check your current username
Basics
git init
Initialize a new git repository
git clone <url>
Clone a remote repository
git add <file>
Stage a file for commit
git add .
Stage all changes for commit
git commit -m "message"
Commit staged changes with a message
git commit -am "message"
Stage and commit tracked files
git status
Show working tree status
git diff
Show unstaged changes
git diff --staged
Show staged changes
Branching
git branch
List local branches
git branch -a
List all branches (local and remote)
git branch <branch-name>
Create a new branch
git checkout <branch-name>
Switch to a different branch
git checkout -b <branch-name>
Create and switch to a new branch
git switch <branch-name>
Modern syntax to switch branches
git switch -c <branch-name>
Modern syntax to create and switch
git branch -d <branch-name>
Delete a branch (safe)
git branch -D <branch-name>
Force delete a branch
git branch -m <new-name>
Rename current branch
git branch -m <old> <new>
Rename a specific branch
Merging
git merge <branch-name>
Merge a branch into current branch
git merge --no-ff <branch-name>
Merge with merge commit (preserves history)
git merge --squash <branch-name>
Squash commits before merging
git rebase <branch-name>
Rebase current branch onto another
git merge --abort
Cancel a merge in progress
Remote Repositories
git remote -v
Show all remote repositories with URLs
git remote add <name> <url>
Add a new remote repository
git remote remove <name>
Remove a remote repository
git remote rename <old> <new>
Rename a remote
git fetch
Fetch all changes from remote
git fetch <remote>
Fetch changes from specific remote
git pull
Fetch and merge remote changes
git pull --rebase
Fetch and rebase instead of merge
git push
Push commits to remote
git push <remote> <branch>
Push specific branch to remote
git push -u origin <branch>
Push and set upstream tracking
git push --all
Push all branches to remote
git push --force
Force push to remote (use with caution)
Stashing
git stash
Stash current changes
git stash save "message"
Stash changes with a message
git stash list
List all stashed changes
git stash pop
Apply and remove latest stash
git stash apply
Apply latest stash without removing
git stash drop
Delete latest stash
git stash clear
Delete all stashes
History & Logs
git log
Show commit history
git log --oneline
Show commit history in one line per commit
git log --graph --oneline --all
Show branch graph with history
git log -p
Show commit history with full diff
git log --author="name"
Filter commits by author
git log --since="2 weeks ago"
Filter commits by date
git show <commit>
Show details of a specific commit
git blame <file>
Show who changed each line
git reflog
Show reference logs (all head movements)
Undoing Changes
git restore <file>
Discard changes in working directory
git restore --staged <file>
Unstage a file
git reset HEAD <file>
Unstage a file (older syntax)
git reset --soft HEAD~1
Undo last commit, keep changes staged
git reset --mixed HEAD~1
Undo last commit, keep changes unstaged
git reset --hard HEAD~1
Undo last commit, discard changes
git revert <commit>
Create new commit that undoes changes
git clean -fd
Remove untracked files and directories
Tags
git tag
List all tags
git tag <tag-name>
Create a lightweight tag
git tag -a <tag> -m "message"
Create an annotated tag
git show <tag>
Show tag details
git tag -d <tag>
Delete a local tag
git push origin <tag>
Push a specific tag to remote
git push origin --tags
Push all tags to remote
Advanced
git cherry-pick <commit>
Apply a specific commit to current branch
git bisect start
Start binary search to find bug-introducing commit
git grep "pattern"
Search for text in tracked files
git worktree add <path> <branch>
Create a separate working directory
git filter-branch
Rewrite commit history (advanced)