Git Cheat Sheet - Essential Commands for Developers
A comprehensive guide to the most commonly used Git commands that every developer should know. This cheat sheet covers everything from basic operations to advanced workflows.
Configuration
Set your identity
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"View configuration
git config --list
git config user.nameSet default editor
git config --global core.editor "code --wait" # VS Code
git config --global core.editor "vim" # VimSet default branch name
git config --global init.defaultBranch mainRepository Setup
Initialize a new repository
git init
git init project-nameClone an existing repository
git clone https://github.com/user/repo.git
git clone https://github.com/user/repo.git custom-name
git clone -b branch-name https://github.com/user/repo.gitBasic Commands
Check repository status
git status
git status -s # Short formatAdd files to staging
git add filename.txt
git add . # Add all files
git add *.js # Add all JS files
git add -A # Add all changes
git add -p # Interactive stagingCommit changes
git commit -m "Commit message"
git commit -am "Add and commit" # Add and commit tracked files
git commit --amend # Modify last commit
git commit --amend -m "New message" # Change last commit messageView differences
git diff # Unstaged changes
git diff --staged # Staged changes
git diff HEAD # All changes since last commit
git diff branch1..branch2 # Differences between branchesBranching
Create and manage branches
git branch # List local branches
git branch -a # List all branches (local + remote)
git branch branch-name # Create new branch
git branch -d branch-name # Delete branch (safe)
git branch -D branch-name # Force delete branch
git branch -m old-name new-name # Rename branchSwitch branches
git checkout branch-name
git checkout -b new-branch # Create and switch
git switch branch-name # Modern alternative
git switch -c new-branch # Create and switch (modern)Merge branches
git merge branch-name
git merge --no-ff branch-name # Create merge commit
git merge --squash branch-name # Squash commits
git merge --abort # Abort mergeRebase
git rebase main
git rebase -i HEAD~3 # Interactive rebase last 3 commits
git rebase --continue # Continue after resolving conflicts
git rebase --abort # Abort rebaseRemote Operations
View remotes
git remote
git remote -v # Show URLs
git remote show origin # Detailed infoAdd/remove remotes
git remote add origin https://github.com/user/repo.git
git remote remove origin
git remote rename old-name new-name
git remote set-url origin https://github.com/user/repo.gitFetch and pull
git fetch origin
git fetch --all # Fetch all remotes
git pull # Fetch and merge
git pull origin main
git pull --rebase # Fetch and rebasePush changes
git push origin branch-name
git push -u origin branch-name # Set upstream
git push --all # Push all branches
git push --tags # Push tags
git push --force # Force push (dangerous!)
git push --force-with-lease # Safer force pushPull from remote and override local changes
git fetch origin
git reset --hard origin/mainStashing
Save and restore changes
git stash # Stash changes
git stash save "message" # Stash with message
git stash -u # Include untracked files
git stash list # List all stashes
git stash show # Show latest stash
git stash show -p # Show latest stash with diff
git stash show -p stash@{1} # Show specific stashApply and remove stashes
git stash apply # Apply latest stash
git stash apply stash@{1} # Apply specific stash
git stash pop # Apply and remove latest
git stash drop # Remove latest stash
git stash drop stash@{1} # Remove specific stash
git stash clear # Remove all stashesCreate branch from stash
git stash branch branch-nameUndoing Changes
Unstage files
git reset HEAD -- path/to/file
git reset HEAD -- . # Unstage all
git restore --staged file # Modern alternativeDiscard changes
git checkout -- file # Discard working directory changes
git restore file # Modern alternative
git clean -fd # Remove untracked files/directories
git clean -fdn # Dry runReset commits
git reset --soft HEAD~1 # Keep changes staged
git reset --mixed HEAD~1 # Keep changes unstaged (default)
git reset --hard HEAD~1 # Discard changes completely
git reset --hard commit-hash # Reset to specific commitRevert commits
git revert commit-hash # Create new commit that undoes changes
git revert HEAD # Revert last commit
git revert HEAD~3..HEAD # Revert range of commitsViewing History
View commit history
git log
git log --oneline # Compact view
git log --graph # Show branch graph
git log --all --graph --oneline # Visual branch history
git log -n 5 # Last 5 commits
git log --author="Name" # Filter by author
git log --since="2 weeks ago"
git log --until="2023-01-01"
git log --grep="keyword" # Search commit messages
git log -- file.txt # Commits affecting fileShow commit details
git show commit-hash
git show HEAD # Show last commit
git show HEAD~2 # Show commit 2 steps backView file history
git blame file.txt # Show who changed each line
git log -p file.txt # Show changes to file
git log --follow file.txt # Follow renamesReference log
git reflog # Show all reference updates
git reflog show branch-nameTagging
Create tags
git tag v1.0.0 # Lightweight tag
git tag -a v1.0.0 -m "Version 1.0.0" # Annotated tag
git tag -a v1.0.0 commit-hash # Tag specific commitView and manage tags
git tag # List tags
git tag -l "v1.*" # Filter tags
git show v1.0.0 # Show tag details
git tag -d v1.0.0 # Delete local tag
git push origin --delete v1.0.0 # Delete remote tagPush tags
git push origin v1.0.0
git push origin --tags # Push all tagsAdvanced Commands
Cherry-pick commits
git cherry-pick commit-hash
git cherry-pick commit1 commit2
git cherry-pick --continue
git cherry-pick --abortSearch in repository
git grep "search-term"
git grep -n "search-term" # Show line numbers
git grep "search-term" main # Search in specific branchBisect (find bugs)
git bisect start
git bisect bad # Current commit is bad
git bisect good commit-hash # Known good commit
git bisect reset # End bisect sessionSubmodules
git submodule add https://github.com/user/repo.git path
git submodule init
git submodule update
git submodule update --remoteWork with patches
git format-patch -1 HEAD # Create patch from last commit
git apply patch-file.patch # Apply patch
git am patch-file.patch # Apply patch with commitTroubleshooting
Resolve merge conflicts
git status # See conflicted files
# Edit files to resolve conflicts
git add resolved-file.txt
git commit # Complete merge
git merge --abort # Abort merge if neededError: You have not concluded your merge (MERGE_HEAD exists)
# Either complete the merge:
git add .
git commit -m "Resolved merge conflicts"
# Or abort the merge:
git merge --abortUncommit last changes but keep files
git reset --soft HEAD~1Remove file from Git but keep locally
git rm --cached filename
git rm -r --cached directory/Change remote URL
git remote set-url origin https://new-url.gitSync fork with upstream
git remote add upstream https://github.com/original/repo.git
git fetch upstream
git checkout main
git merge upstream/mainFix “detached HEAD” state
git checkout -b new-branch # Create branch from current state
# Or return to a branch:
git checkout mainRecover deleted commits
git reflog
git checkout -b recovery-branch commit-hashUseful Aliases
Add these to your .gitconfig file or use git config --global alias.name 'command':
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status
git config --global alias.unstage 'reset HEAD --'
git config --global alias.last 'log -1 HEAD'
git config --global alias.visual 'log --all --graph --oneline --decorate'Best Practices
- Commit often with clear, descriptive messages
- Pull before push to avoid conflicts
- Create feature branches for new work
- Review changes before committing (
git diff) - Don’t force push to shared branches
- Use .gitignore to exclude unnecessary files
- Keep commits atomic - one logical change per commit
- Write meaningful commit messages - explain why, not what
Common Workflows
Feature branch workflow
git checkout main
git pull origin main
git checkout -b feature/new-feature
# Make changes
git add .
git commit -m "Add new feature"
git push -u origin feature/new-feature
# Create pull request, then after merge:
git checkout main
git pull origin main
git branch -d feature/new-featureHotfix workflow
git checkout main
git checkout -b hotfix/critical-bug
# Fix the bug
git add .
git commit -m "Fix critical bug"
git checkout main
git merge hotfix/critical-bug
git push origin main
git branch -d hotfix/critical-bugThis cheat sheet covers the essential Git commands you’ll use daily as a developer. Keep it handy for quick reference!