Git Commands Cheatsheet

Essential Git Commands for Version Control

Category: Version Control
Level: Beginner — Intermediate

Complete Git commands cheatsheet covering setup, staging, committing, branching, merging, remotes, undoing changes, and stashing. Bookmark this quick reference.

🔧 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 to main
git config --global init.defaultBranch main

# Set VS Code as default editor
git config --global core.editor "code --wait"

# View all configuration
git config --list

# View specific setting
git config user.name

# Store credentials (avoid re-entering password)
git config --global credential.helper store

📁 Creating & Cloning Repositories

# Initialize a new repo in current directory
git init

# Initialize with a specific name
git init my-project

# Clone a remote repository
git clone https://github.com/user/repo.git

# Clone into a specific folder name
git clone https://github.com/user/repo.git my-folder

# Clone a specific branch only
git clone -b main --single-branch https://github.com/user/repo.git

# Clone with all submodules
git clone --recurse-submodules https://github.com/user/repo.git

📸 Staging & Committing

# Check repository status
git status

# Stage a specific file
git add filename.txt

# Stage all changes in current directory
git add .

# Stage all changes in the entire repo
git add -A

# Stage specific parts of a file interactively
git add -p filename.txt

# Commit staged changes with a message
git commit -m "Your commit message"

# Stage all tracked files and commit in one step
git commit -am "Your commit message"

# Amend the last commit message (before pushing)
git commit --amend -m "Corrected commit message"

# Amend last commit and add staged changes (before pushing)
git commit --amend --no-edit

# View commit history
git log

# View compact one-line history
git log --oneline

# View history with branch graph
git log --oneline --graph --all

# View changes not yet staged
git diff

# View changes already staged (ready to commit)
git diff --staged

# View diff for a specific file
git diff filename.txt

🌿 Branching

# List all local branches
git branch

# List all branches (local + remote)
git branch -a

# Create a new branch
git branch feature/new-feature

# Switch to a branch
git switch feature/new-feature

# Create and switch in one command
git switch -c feature/new-feature

# Rename current branch
git branch -m new-name

# Delete a branch (safe — won't delete unmerged)
git branch -d feature/old-feature

# Force delete a branch (even if unmerged)
git branch -D feature/abandoned-feature

# View last commit on each branch
git branch -v

🔀 Merging

# Merge a branch into current branch
git merge feature/new-feature

# Merge without fast-forward (preserves branch history)
git merge --no-ff feature/new-feature

# Abort a merge in progress (when conflicts exist)
git merge --abort

# After resolving conflicts — mark as resolved and complete
git add resolved-file.txt
git commit

# View branches already merged into current branch
git branch --merged

# View branches NOT yet merged
git branch --no-merged

🔁 Rebasing

# Rebase current branch onto main
git rebase main

# Interactive rebase — last 3 commits
git rebase -i HEAD~3

# Continue after resolving rebase conflict
git rebase --continue

# Skip a commit during rebase
git rebase --skip

# Abort rebase and return to original state
git rebase --abort

When to use rebase vs merge:
Rebase = cleaner linear history, best for local feature branches before merging.
Merge = preserves full history with branch context. Never rebase commits already pushed to a shared remote.

🌐 Remote Operations

# View remote connections
git remote -v

# Add a remote
git remote add origin https://github.com/user/repo.git

# Change remote URL
git remote set-url origin https://github.com/user/new-repo.git

# Remove a remote
git remote remove origin

# Fetch changes from remote (doesn't merge)
git fetch origin

# Fetch all remotes
git fetch --all

# Pull changes (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 (first push)
git push -u origin feature/new-feature

# Push all branches
git push --all origin

# Delete a remote branch
git push origin --delete feature/old-branch

# Push tags to remote
git push origin --tags

↩️ Undoing Changes

# Discard changes in a file (back to last commit)
git restore filename.txt

# Unstage a file (keep changes, just remove from staging)
git restore --staged filename.txt

# 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 (destructive)
git reset --hard HEAD~1

# Revert a commit (creates a new "undo" commit — safe for shared branches)
git revert abc1234

# Revert without auto-commit
git revert --no-commit abc1234

# Remove untracked files (preview first)
git clean -n

# Remove untracked files (execute)
git clean -f

# Remove untracked files and directories
git clean -fd

🗄️ Stashing

# Stash current changes (staged and unstaged)
git stash

# Stash with a descriptive name
git stash push -m "work in progress: login feature"

# Stash including untracked files
git stash -u

# List all stashes
git stash list

# Apply most recent stash (keep it in stash list)
git stash apply

# Apply most recent stash and remove it from list
git stash pop

# Apply a specific stash
git stash apply stash@{2}

# Drop a specific stash
git stash drop stash@{1}

# Clear all stashes
git stash clear

# Create a branch from a stash
git stash branch feature/from-stash stash@{0}

🏷️ Tagging

# List all tags
git tag

# 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"

# Tag a specific commit
git tag -a v1.0.0 abc1234 -m "Release"

# Push a tag to remote
git push origin v1.0.0

# Push all tags
git push origin --tags

# Delete a local tag
git tag -d v1.0.0

# Delete a remote tag
git push origin --delete v1.0.0

🔍 Searching & Inspecting

# Search for a string across all files
git grep "search term"

# Find which commit introduced a string
git log -S "function name" --oneline

# Show details of a specific commit
git show abc1234

# Show who changed each line of a file
git blame filename.txt

# Find which commit introduced a bug (binary search)
git bisect start
git bisect bad          # current commit is bad
git bisect good v1.0.0  # last known good commit
# Git checks out commits for you to test
git bisect good         # or: git bisect bad
git bisect reset        # when done

⚡ Useful Aliases (Add to ~/.gitconfig)

git config --global alias.st status
git config --global alias.co "switch"
git config --global alias.br branch
git config --global alias.lg "log --oneline --graph --all --decorate"
git config --global alias.unstage "restore --staged"
git config --global alias.last "log -1 HEAD --stat"
git config --global alias.aliases "config --get-regexp alias"

→ Related: DevOps Basics Guide | Deploy to AWS with Docker Tutorial | Version Control Glossary Term

📬 New Cheatsheets Added Regularly

New quick-reference guides are added as the hub grows. Subscribe to the newsletter to know when new ones drop.

→ Subscribe to the Newsletter