DEV Community

楊東霖
楊東霖

Posted on • Originally published at devtoolkit.cc

Git Cheat Sheet: 60+ Commands Every Developer Should Know

Git is the foundation of modern software development, but its command surface area is enormous. This cheat sheet organizes every command you'll actually use — from daily workflow basics to advanced history rewriting — so you can find what you need quickly and understand what it does.

Setup and Configuration

# Set your identity (required for commits)
git config --global user.name "Your Name"
git config --global user.email "you@example.com"

# Set default editor (VS Code shown)
git config --global core.editor "code --wait"

# Set default branch name for new repos
git config --global init.defaultBranch main

# Enable color output
git config --global color.ui auto

# View all config settings
git config --list

# Create a .gitignore template
git config --global core.excludesfile ~/.gitignore_global
Enter fullscreen mode Exit fullscreen mode

Creating and Cloning Repositories

# Initialize a new repo in the current directory
git init

# Initialize with a specific branch name
git init -b main

# 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
git clone -b develop https://github.com/user/repo.git

# Shallow clone (faster, no full history)
git clone --depth 1 https://github.com/user/repo.git
Enter fullscreen mode Exit fullscreen mode

Staging and Committing

# Show working tree status
git status
git status -s   # short format

# Stage specific file
git add filename.txt

# Stage all changes in current directory
git add .

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

# Commit with a message
git commit -m "feat: add user authentication"

# Stage all tracked changes and commit in one step
git commit -am "fix: correct off-by-one error"

# Amend the last commit (message or staged changes)
git commit --amend -m "corrected message"
git commit --amend --no-edit   # keep same message

# Create an empty commit (useful for triggering CI)
git commit --allow-empty -m "chore: trigger pipeline"
Enter fullscreen mode Exit fullscreen mode

Viewing History and Diffs

# Show commit history
git log

# Compact one-line format
git log --oneline

# Visual branch graph
git log --oneline --graph --all

# Show last N commits
git log -5

# Show commits by a specific author
git log --author="Alice"

# Show commits with a string in the message
git log --grep="bug fix"

# Show what changed in each commit (with diffs)
git log -p

# Show changes between commits
git diff HEAD~1 HEAD

# Show staged changes (what will be committed)
git diff --staged
git diff --cached   # same thing

# Show unstaged changes
git diff

# Show changes for a specific file
git diff main..feature -- src/app.js

# Show who changed each line (blame)
git blame filename.js

# Show what a commit changed
git show abc1234
Enter fullscreen mode Exit fullscreen mode

Branching

# List all branches
git branch          # local
git branch -r       # remote
git branch -a       # all

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

# Create and switch in one step
git checkout -b feature/new-feature   # classic
git switch -c feature/new-feature     # modern (Git 2.23+)

# Switch to an existing branch
git checkout main
git switch main                       # modern

# Rename a branch
git branch -m old-name new-name

# Delete a merged branch
git branch -d feature/done

# Force delete an unmerged branch
git branch -D feature/abandoned

# List branches sorted by most recent commit
git branch --sort=-committerdate
Enter fullscreen mode Exit fullscreen mode

Merging

# Merge a branch into the current branch
git merge feature/my-feature

# Merge with a commit message (no fast-forward)
git merge --no-ff feature/my-feature -m "Merge feature/my-feature"

# Fast-forward only (fail if not possible)
git merge --ff-only feature/hotfix

# Merge and squash all commits into one
git merge --squash feature/my-feature
git commit -m "feat: implement my feature"

# Abort a merge in progress
git merge --abort

# View unmerged branches
git branch --no-merged

# View already-merged branches
git branch --merged
Enter fullscreen mode Exit fullscreen mode

Rebasing

# Rebase current branch onto main
git rebase main

# Interactive rebase — rewrite, squash, reorder last N commits
git rebase -i HEAD~3
git rebase -i main

# Interactive rebase actions:
# pick   — keep the commit as-is
# reword — keep but change the message
# edit   — pause to amend
# squash — combine with previous commit
# fixup  — like squash but discard this message
# drop   — delete the commit

# Continue after resolving conflicts
git rebase --continue

# Abort a rebase in progress
git rebase --abort

# Rebase onto a different base
git rebase --onto main feature-base feature-tip
Enter fullscreen mode Exit fullscreen mode

Undoing Changes

# Unstage a file (keep changes in working directory)
git restore --staged filename.txt   # modern
git reset HEAD filename.txt         # classic

# Discard changes in working directory
git restore filename.txt   # ⚠️ irreversible
git checkout -- filename.txt

# Undo the last commit (keep changes staged)
git reset --soft HEAD~1

# Undo the last commit (keep changes unstaged)
git reset --mixed HEAD~1

# Undo the last commit (discard changes) ⚠️ destructive
git reset --hard HEAD~1

# Revert a commit (creates a new commit that undoes it — safe for shared history)
git revert abc1234
git revert HEAD    # revert the last commit

# Revert a merge commit
git revert -m 1 abc1234

# Remove untracked files
git clean -n       # dry run — see what would be deleted
git clean -f       # delete untracked files
git clean -fd      # delete untracked files and directories
git clean -fdx     # also delete gitignored files ⚠️
Enter fullscreen mode Exit fullscreen mode

Stashing

# Stash uncommitted changes
git stash
git stash push -m "work in progress: user form"

# Include untracked files in stash
git stash -u

# List all stashes
git stash list

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

# Apply and remove the most recent stash
git stash pop

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

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

# Delete all stashes
git stash clear

# Show what's in a stash
git stash show -p stash@{0}

# Create a branch from a stash
git stash branch feature/from-stash stash@{0}
Enter fullscreen mode Exit fullscreen mode

Remote Operations

# List remote connections
git remote -v

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

# Rename a remote
git remote rename origin upstream

# Remove a remote
git remote remove upstream

# Fetch changes (don't merge)
git fetch origin
git fetch --all   # fetch all remotes

# Pull (fetch + merge)
git pull origin main
git pull --rebase origin main   # fetch + rebase instead of merge

# Push a branch
git push origin feature/my-feature

# Push and set upstream tracking
git push -u origin feature/my-feature

# Force push (rewrite remote history) ⚠️ dangerous on shared branches
git push --force-with-lease   # safer: fails if remote has new commits
git push --force              # ⚠️ use with caution

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

# Push all branches
git push --all origin

# Push all tags
git push --tags
Enter fullscreen mode Exit fullscreen mode

Tagging

# List all tags
git tag
git tag -l "v1.*"

# 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 v0.9.0 abc1234

# Show tag information
git show v1.0.0

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

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

# Delete a remote tag
git push origin --delete v1.0.0
Enter fullscreen mode Exit fullscreen mode

Cherry-picking

# Apply a specific commit to the current branch
git cherry-pick abc1234

# Cherry-pick without committing
git cherry-pick -n abc1234

# Cherry-pick a range of commits
git cherry-pick abc1234..def5678

# Continue after resolving conflicts
git cherry-pick --continue

# Abort
git cherry-pick --abort
Enter fullscreen mode Exit fullscreen mode

Searching

# Search for a string in the working tree
git grep "TODO"

# Search in a specific commit
git grep "function login" v1.0.0

# Find the commit that 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 a middle commit — test and mark:
git bisect good
git bisect bad
# ... repeat until Git identifies the culprit
git bisect reset         # return to HEAD
Enter fullscreen mode Exit fullscreen mode

Advanced: Reflog and Recovery

# Show a log of all ref updates — your safety net
git reflog

# Recover a deleted branch
git reflog                     # find the commit SHA
git checkout -b recovered abc1234   # restore it

# Recover from a bad reset
git reset --hard abc1234       # go back to the right commit
Enter fullscreen mode Exit fullscreen mode

Useful Aliases

# Add these to your ~/.gitconfig
[alias]
  st = status -sb
  co = checkout
  sw = switch
  lg = log --oneline --graph --all
  undo = reset --soft HEAD~1
  unstage = restore --staged
  aliases = config --list --global | grep alias
Enter fullscreen mode Exit fullscreen mode

Git Workflow Summary

# Feature branch workflow
git switch -c feature/my-feature     # create branch
# ... make changes ...
git add .
git commit -m "feat: implement feature"
git push -u origin feature/my-feature  # push
# open pull request on GitHub/GitLab
# after review and merge:
git switch main
git pull origin main                   # update local
git branch -d feature/my-feature       # clean up
Enter fullscreen mode Exit fullscreen mode

Keep this cheat sheet bookmarked and you'll spend less time searching for command syntax and more time writing code. For complex operations like rebase and reset, always double-check whether your changes are on a shared branch before running them — history rewriting on shared branches causes pain for everyone on the team.

Free Developer Tools

If you found this article helpful, check out DevToolkit — 40+ free browser-based developer tools with no signup required.

Popular tools: JSON Formatter · Regex Tester · JWT Decoder · Base64 Encoder

🛒 Get the DevToolkit Starter Kit on Gumroad — source code, deployment guide, and customization templates.

Top comments (0)