Git CLI Cheat Sheet
Quick reference for the most commonly used Git commands for branching, merging, and for productivity.
| Category |
Description |
Command |
| Configuration |
User name, email, global config |
git config --global user.name "John Doe"
git config --global user.email "john@example.com" |
| Branching |
Create, switch, delete branches |
git checkout -b feature
git checkout -b bugfix |
| Branching |
List branches |
git branch |
| Stash |
Save changes |
git stash save "work in progress"
git stash save -u |
| Synchronization |
Push changes to remote, fetch latest changes |
git fetch origin
git pull |
| Status |
Show working tree status |
git status
git status -s (shows staged changes) |
| Remote Management |
Add remote origin |
git remote add origin <url>
git remote -v |
| History |
View commit history |
git log --oneline
git log --oneline -n
git log -p patch-1 --pretty
git log -p --graph
git log --stat
git log --all
git log --follow --log |
| History |
Follow the tail of commit in real-time |
git log --oneline --decorate |
| History |
Show commit log |
git log --oneline |
Basic Commands
| Command |
Description |
Example |
git init |
Initialize new repository |
git init |
git clone <url> |
Clone repository |
git clone https://github.com/user/repo.git |
git add <files> |
Stage changes |
git add . or git add -p |
git commit -m "msg" |
Commit changes |
git commit -m "Initial commit" |
git commit --amend |
Modify last commit |
git commit --amend -m "Fix typo" |
git reset <file> |
Discard changes |
git reset HEAD --hard |
git rm <file> |
Remove file |
git rm filename.txt |
git mv <old> <new> |
Move/rename file |
git mv old.txt new.txt |
git checkout <branch> |
Switch branches |
git checkout main
git checkout develop |
git checkout -b <branch> |
Create and switch |
git checkout -b bugfix |
git merge <branch> |
Merge branches |
git merge main feature
git merge --no-ff bugfix |
git branch -d <name> |
Delete branch |
git branch -d feature/branch
git branch -D bugfix/branch-name |
git branch -D <name> |
Delete remote-tracking branch |
git branch -r origin/remote |
git branch -m <name> |
Rename branch |
git branch -m "message"
git branch -m "switch to progress branch" |
git branch |
Delete local branch |
git branch -d local-branch-name |
git branch -r origin/remote <name> |
Push remote branch and pull remote changes |
git branch --set-upstream origin/main |
git branch --set-upstream origin <branch-name> |
Set upstream branch |
git branch --set-upstream origin/main |
Branching Strategies
Use branches to isolate feature development and create feature branches for bug fixes.
| Strategy |
Command |
| Feature Branch |
git checkout -b feature/login-page |
| Bug Fix Branch |
git checkout -b bugfix/loading-crash |
| Hotfix Branch |
git checkout -b hotfix/new-header |
| Release Branch |
git checkout -b release/v2.0 |
| Experimental |
git checkout -b experiment/new-ui |
| Git Flow | Enable Git Flow for Linter | git flow init |
git branch --set-upstream-to=origin/main login-page
git flow init
git branch -u origin/main features/login-page
Branch Management
| Command |
Description |
Example |
git branch |
List all branches |
git branch -a |
git branch -d <name> |
Delete branch |
git branch -D branch-name |
git branch -m <name> |
Rename branch |
git branch -m old-name new-name |
git branch -r <name> <remote> |
List remote branches |
git branch -r |
git branch -r -d <name> |
Delete remote branch |
git branch -r -d branch-name |
Stashing & Saving
| Command |
Description |
Example |
git stash |
Save local changes |
git stash save "work in progress" |
git stash list |
List stashes |
git stash list |
git stash apply |
Apply stash |
git stash pop |
git stash apply |
Apply stash |
git stash apply |
git stash drop |
Delete stash |
git stash drop |
git stash clear |
Clear stash |
git stash clear |
Remote Operations
| Command |
Description |
Example |
git fetch |
Download objects and refs from remote |
git fetch origin |
git pull |
Fetch and merge remote changes |
git pull origin main |
git push |
Push local changes to remote |
git push origin main |
git push --force |
Force push (use with caution) |
git push --force origin main |
git push --set-upstream origin |
Push and set upstream tracking |
git push --set-upstream origin main |
git push --all |
Push all branches |
git push --all --force |
git clone --recurse-submodules |
Clone with submodules |
git clone --recurse-submodules <url> |
git submodule update |
Update submodules |
git submodule update --init |
git submodule update --remote |
Update from remote |
git submodule update --remote |
Tags
| Tag |
Description |
Example |
v1.0.0 |
Lightweight tag for releases |
git tag v1.0.0 |
v2.0.0 |
Lightweight annotated tag |
git tag -a v2.0.0 |
git tag -a v1.0.0 <message> |
List tags with message |
git tag -l "v1.0.0" |
git tag -d v1.0.0 |
Delete tag |
git tag -d v1.0.0 |
git push origin --tags |
Push tags |
git push origin --tags v1.0.0 v2.0.0 |
git push --follow-tags |
Push tags and follow them |
git push --follow-tags |
git tag -d v1.0.0 |
Delete tag locally and push to remote |
git push --follow-tags origin :main |
Rebase & Reset
| Command |
Description |
Example |
git rebase -i |
Interactive rebase |
Not recommended |
git rebase <branch> |
Rebase current branch onto another |
git rebase main |
git rebase --abort |
Abort ongoing rebase |
git rebase --abort |
git rebase --continue |
Continue after resolving conflicts |
git rebase --continue |
git rebase --skip |
Skip patch and rebase manually |
git rebase --skip |
Cherry-pick
| Command |
Description |
Example |
git cherry-pick <commit> |
Pick specific commits |
git cherry-pick <commit-hash> |
git cherry-pick --continue |
Continue after conflicts |
git cherry-pick --continue |
git cherry-pick --quit |
Quit cherry-pick |
git cherry-pick --quit |
git cherry-pick --abort |
Abort cherry-pick |
git cherry-pick --abort |
| ----------- |
------------- |
|
| Command |
Description |
Example |
| ----------- |
------------- |
--------- |
git clean -fd |
Clean untracked files |
git clean -fd |
git clean -fx |
Clean ignored files |
git clean -fx |
| git gc | Garbage collection | git gc |
Bisect
| Command |
Description |
Example |
git bisect |
Binary search for commits |
git bisect start
git bisect good
git bisect bad
git bisect reset
git bisect replay
git bisect log
git bisect run <cmd...>
git bisect visualize |
git blame |
Show what revision modified a line |
git blame <file> |
Worktree
| Command |
Description |
Example |
git worktree add |
Add new worktree |
git worktree add <path> <branch> |
git worktree list |
List worktrees |
git worktree list |
git worktree remove |
Remove worktree |
git worktree remove <path> |
git worktree prune |
Prune deleted worktrees |
git worktree prune |
Reflog
| Command |
Description |
Example |
git reflog |
Show reference log |
git reflog
git reflog show
git reflog expire |
git reset --hard HEAD@{n} |
Reset to previous commit |
git reset --hard HEAD~5 |
git reset --soft HEAD@{n} |
Reset but keep changes |
git reset --soft HEAD~5 |
git revert HEAD~{n} |
Undo commit but keep changes |
git revert HEAD~5 |
Ignore
| Command |
Description |
Example |
git ignore |
Ignore files |
Add *.log to .gitignore
git ignore *.env |
git check-ignore -v |
Check ignore status |
git check-ignore -v |
git rm --cached |
Remove from index |
git rm --cached -r .env |
Submodules
| Command |
Description |
Example |
git submodule |
Manage submodules |
git submodule status |
git submodule add <url> |
Add submodule |
git submodule add https://github.com/user/repo.git |
git submodule update --init |
Initialize submodules |
git submodule update --init --recursive |
git submodule update --remote |
Update submodules |
git submodule update --remote |
git submodule foreach |
Run command in each submodule |
git submodule foreach 'git pull' |
SSH/GPG
| Command |
Description |
Example |
git ssh-key |
Manage SSH keys |
ssh-keygen -t ed25519 -C ed25519 -f ~/.ssh/id_ed25519
git config --global gpg.program gpg |
git config --global gpg.program gpg2 |
Configure GPG program |
git config --global gpg.program gpg2 |
git config --global commit.gpgsign true |
Enable GPG signing |
git config --global commit.gpgsign true |
git config --global user.signingkey ~/.ssh/id_ed25519.pub |
Set signing key |
git config --global user.signingkey ~/.ssh/id_ed25519.pub |
CI/CD Integration
| Command |
Description |
Example |
git hooks |
Manage Git hooks |
git config --global core.hooksPath .githooks |
pre-commit |
Run tests before commit |
.git/hooks/pre-commit |
git commit --no-verify |
Skip pre-commit hook |
git commit --no-verify -m "message" |
Troubleshooting
| Command |
Description |
Example |
git fsck |
Check repository integrity |
git fsck |
git prune |
Remove unreachable objects |
git prune |
git count-objects -v |
Count objects |
git count-objects -v |
Tips
Use Aliases for Efficiency
# Add common aliases to ~/.bashrc or ~/.zshrc
alias gs='git status'
alias ga='git add'
alias gc='git commit'
alias gp='git push'
alias gl='git log --oneline'
alias gd='git diff'
alias gco='git checkout'
alias gb='git branch'
alias gm='git merge'
Undo Local Changes
# Discard all local changes
git checkout -- .
git clean -fd
Find Who Changed a Line
# Find commits that modified a specific line
git log -p --follow -S <file> | git blame <file>
git log -p -- "<line>" <file>
# Find when a line was added
git log -p -S <file> | git blame <file>
Sync with Remote
# Fetch and rebase
git fetch origin
git rebase origin/main
# Pull with rebase
git pull --rebase origin main
Clean Start Over
# Remove untracked files and directories
git clean -fd
git clean -fx
Fix Commit Issues
# Undo last commit (keep changes)
git reset --soft HEAD~1
# Undo last commit (discard changes)
git reset --hard HEAD~1
# Amend last commit message
git commit --amend -m "New message"
# Interactive rebase
git rebase -i HEAD~5
Work with Branches
# Create feature branch
git checkout -b feature/awesome-feature
# Push new branch
git push -u origin feature/awesome-feature
# Delete local branch
git branch -d feature/old-feature
# Delete remote branch
git branch -r origin/old-feature
Common Workflows
Start New Feature
# Create and switch to new branch
git checkout -b feature/new-feature
# Stage changes
git add .
git commit -m "Add new feature"
# Push to remote
git push -u origin feature/new-feature
Fix a Bug
# Create hotfix branch
git checkout -b bugfix/issue-123
# Fix the issue
git add .
git commit -m "Fix issue #123"
# Push and merge
git push -u origin bugfix/issue-123
git checkout main
git merge bugfix/issue-123
git push
Release New Version
# Create release branch
git checkout -b release/v2.0.0
# Bump version
git add package.json
git commit -m "Bump to version 2.0.0"
git tag -a v2.0.0
git push origin v2.0.0
git push --tags
Configuration
Set Global Config
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git config --global core.editor "code --wait"
git config --global init.defaultBranch main
Set Local Config
git config user.name "Your Name"
git config user.email "you@example.com"
View Config
git config --list
git config --global --list
git config --local --list
Troubleshooting
Repository is Broken
# Remove .git and restore
rm -rf .git
git init
git remote add origin
Committed to Wrong Branch
# Reset to correct branch
git fetch origin
git reset --hard origin/main
Merges Going Wrong
# Abort merge
git merge --abort
# Reset to remote state
git fetch origin
git reset --hard origin/main
Accidental Commit
# Undo but keep changes
git reset --soft HEAD~1
# Change message
git commit --amend -m "Correct message"
Detached HEAD
# Find commits not in any branch
git log --all --oneline
# Reattach to a branch
git checkout <branch>
Large File in History
# Shallow clone (faster)
git clone --depth 1 <url>
# Full history
git clone <url>
Push Rejected
# Force push (dangerous)
git push --force origin main
# Safer: rebase and push
git rebase origin/main
git push
Next Steps