Git is the world's most widely used version control system. Whether you're a solo developer or working in a team of hundreds, knowing Git deeply makes you significantly more productive and professional. This cheat sheet covers all 50 essential Git commands with explanations and real examples.
Git Setup & Configuration
# Set your identity (required before first commit)
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
# Set default branch name
git config --global init.defaultBranch main
# Set default editor (VS Code)
git config --global core.editor "code --wait"
# View all config settings
git config --list
# Store credentials (avoid retyping password)
git config --global credential.helper storeCreating & Cloning Repositories
# Initialize a new Git repo in current directory
git init
# Initialize with a specific branch name
git init -b main
# Clone a remote repository
git clone https://github.com/username/repo.git
# Clone into a specific folder
git clone https://github.com/username/repo.git my-folder
# Clone a specific branch
git clone -b develop https://github.com/username/repo.gitStaging & Committing
# Check status of working directory
git status
git status -s # short format
# Stage a specific file
git add filename.txt
# Stage all changes
git add .
git add -A # includes deletions
# Stage hunks interactively (pick specific lines)
git add -p
# Commit staged changes
git commit -m "Your descriptive commit message"
# Stage and commit in one step (tracked files only)
git commit -am "Message"
# Amend the last commit (fix message or add files)
git commit --amend -m "Corrected message"
# Commit with detailed multi-line message
git commit # opens editorViewing History & Diffs
# View commit history
git log
git log --oneline # compact format
git log --oneline --graph --all # branch tree view
# Show what changed in a commit
git show abc1234
# Compare working directory to last commit
git diff
# Compare staged changes to last commit
git diff --staged
# Compare two branches
git diff main..feature-branch
# Find which commit introduced a line
git blame filename.txtBranching
# List all local branches
git branch
# List all branches (local + remote)
git branch -a
# Create a new branch
git branch feature/login
# Create and switch to new branch
git checkout -b feature/login
git switch -c feature/login # modern syntax
# Switch to an existing branch
git checkout main
git switch main # modern syntax
# Rename a branch
git branch -m old-name new-name
# Delete a branch (safe โ only if merged)
git branch -d feature/login
# Force delete a branch (even if not merged)
git branch -D feature/loginMerging & Rebasing
# Merge a branch into current branch
git merge feature/login
# Merge without fast-forward (keeps branch history visible)
git merge --no-ff feature/login
# Abort a merge that has conflicts
git merge --abort
# Rebase current branch onto main
git rebase main
# Interactive rebase (squash, edit, reorder commits)
git rebase -i HEAD~3 # last 3 commits
# Abort a rebase
git rebase --abort
# Cherry-pick a specific commit onto current branch
git cherry-pick abc1234Remote Repositories
# List remote connections
git remote -v
# Add a remote
git remote add origin https://github.com/username/repo.git
# Change remote URL
git remote set-url origin https://github.com/username/new-repo.git
# Fetch changes from remote (doesn't merge)
git fetch origin
# Pull = fetch + merge
git pull origin main
# Pull with rebase instead of merge
git pull --rebase origin main
# Push to remote
git push origin main
# Push and set upstream tracking
git push -u origin feature/login
# Push all branches
git push --all origin
# Delete a remote branch
git push origin --delete feature/login
# Force push (use with extreme caution!)
git push --force-with-lease origin feature/loginStashing
# Stash current changes (save for later)
git stash
# Stash with a descriptive name
git stash push -m "WIP: login form validation"
# List all stashes
git stash list
# Apply most recent stash (keeps it in list)
git stash apply
# Apply and remove from stash list
git stash pop
# Apply a specific stash
git stash apply stash@2
# Delete a specific stash
git stash drop stash@0
# Clear all stashes
git stash clearUndoing Mistakes
# Discard changes to a file (unmodified it)
git checkout -- filename.txt
git restore filename.txt # modern syntax
# Unstage a file (undo git add)
git reset HEAD filename.txt
git restore --staged filename.txt # modern syntax
# Undo last commit (keep changes staged)
git reset --soft HEAD~1
# Undo last commit (keep changes unstaged)
git reset --mixed HEAD~1
# Undo last commit (DISCARD all changes โ dangerous!)
git reset --hard HEAD~1
# Create a new commit that undoes a specific commit (safe)
git revert abc1234Tagging
# Create a lightweight tag
git tag v1.0.0
# Create an annotated tag (recommended for releases)
git tag -a v1.0.0 -m "Release version 1.0.0"
# List all tags
git tag
# Push tags to remote
git push origin --tags
# Delete a tag locally
git tag -d v1.0.0
# Delete a tag remotely
git push origin --delete v1.0.0Advanced Git Commands
# Search commit messages
git log --grep="fix bug"
# Search code changes (who wrote this line?)
git log -S "function loginUser"
# Find the commit that introduced a bug (binary search)
git bisect start
git bisect bad # current commit is bad
git bisect good v1.0 # v1.0 was good
# Clean untracked files
git clean -n # dry run (preview)
git clean -fd # delete untracked files and directories
# View the object database
git cat-file -p HEAD
# Sparse checkout (only clone specific folders)
git sparse-checkout init --cone
git sparse-checkout set src/apiGit Aliases (Save Time)
# Add to ~/.gitconfig or run as git config --global alias.X
git config --global alias.st status
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.lg "log --oneline --graph --all --decorate"
git config --global alias.unstage "restore --staged"
# Now use:
git st # instead of git status
git lg # beautiful branch graphCommon Git Workflows
Feature Branch Workflow (most common)
git switch main && git pull
git switch -c feature/new-thing
# ... make changes ...
git add . && git commit -m "feat: add new thing"
git push -u origin feature/new-thing
# Open Pull Request โ review โ merge โ delete branchFixing a Mistake You Just Pushed
# NEVER force push to shared branches. Instead:
git revert abc1234 # creates a new undo commit
git push origin main # safe for everyoneConclusion
Mastering these 50 Git commands will make you significantly more effective as a developer. The most important ones to internalize are: add, commit, push, pull, branch, merge, stash, and rebase. Practice them daily and Git will become second nature.
More dev tools: UUID Generator ยท JSON Formatter ยท Dev Cheatsheets