Git is the de facto standard for version control, used by millions of developers and nearly every software project on the planet. But despite being ubiquitous, many developers only know a handful of commands — enough to get by, but not enough to work efficiently.
This guide covers the 50 most important Git commands, organized by task. You'll learn not just what each command does, but when to use it and why it matters. Whether you're a beginner still confused by git add vs git commit, or an experienced developer who's never touched git reflog, this cheatsheet has something for you.
1. Setup and Configuration
These commands set up your identity and configure Git's behavior. Run them once when you first install Git.
git config
Configure Git settings. The --global flag applies the setting to all repositories on your machine.
# Set your name and email (required for commits)
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
# Set your default editor
git config --global core.editor "code --wait"
# Set default branch name to 'main'
git config --global init.defaultBranch main
# View all config settings
git config --list
# View a specific setting
git config user.email
git init
Initialize a new Git repository in the current directory. Creates a hidden .git folder that stores all version history.
# Initialize in the current directory
git init
# Initialize a new named directory
git init my-project
git clone
Copy a remote repository to your local machine. This is how you start working on an existing project.
# Clone a repository
git clone https://github.com/user/repo.git
# Clone into a specific directory name
git clone https://github.com/user/repo.git my-folder
# Clone only the latest snapshot (shallow clone, faster for large repos)
git clone --depth 1 https://github.com/user/repo.git
2. Staging and Committing
This is the core of daily Git work — preparing changes and saving them to history. Git uses a three-stage model: working directory → staging area → repository.
git status
Shows the current state of your working directory and staging area. Run this constantly — it's free and informative.
git status
# Compact output (one file per line)
git status -s
git add
Stage changes — moves files from the working directory to the staging area, ready to be committed.
# Stage a specific file
git add index.html
# Stage all changes in the current directory
git add .
# Stage all tracked files with changes
git add -u
# Interactively choose which changes to stage (within a file)
git add -p
git commit
Save staged changes to the repository with a message describing what changed and why.
# Open editor for commit message
git commit
# Commit with an inline message
git commit -m "Add login form validation"
# Stage all tracked changes and commit in one step
git commit -am "Fix typo in README"
# Amend the last commit (change message or add more changes)
git commit --amend
Write better commit messages: Use the imperative mood ("Add feature" not "Added feature"), keep the subject under 72 characters, and explain why — not what — in the body.
git diff
Show what changed between different versions of your code.
# Show unstaged changes (working dir vs staging area)
git diff
# Show staged changes (staging area vs last commit)
git diff --staged
# Compare two branches
git diff main feature-branch
# Show stats only (files changed, insertions, deletions)
git diff --stat HEAD~1
git rm and git mv
Remove or move files in a way Git understands. Using regular rm or mv creates an untracked change that requires extra cleanup.
# Remove a file from Git tracking and the filesystem
git rm old-file.txt
# Remove from Git tracking but keep the file locally
git rm --cached secret.env
# Rename / move a file
git mv old-name.txt new-name.txt
3. Viewing History
git log
Browse the commit history. One of the most-used commands — master a few flags to get useful output fast.
# Default log (verbose)
git log
# One line per commit
git log --oneline
# Visualize branch topology
git log --oneline --graph --decorate --all
# Show commits by a specific author
git log --author="Alice"
# Show commits that touched a specific file
git log --follow -- src/auth.js
# Show commits in a date range
git log --after="2026-01-01" --before="2026-03-01"
git show
Display details of a specific commit: the message, author, date, and full diff.
# Show the latest commit
git show
# Show a specific commit by hash
git show a1b2c3d
# Show a file as it was in a specific commit
git show a1b2c3d:src/index.js
git blame
Show who last modified each line of a file. Invaluable when tracking down when and why a particular line was changed.
# Annotate every line of a file
git blame src/auth.js
# Ignore whitespace changes
git blame -w src/auth.js
# Show blame for a specific line range
git blame -L 10,30 src/auth.js
git shortlog
Summarize commit history by author. Great for release notes and contributor stats.
# Summary of commits per author
git shortlog -sn
# Summary since a specific tag
git shortlog -sn v1.0..HEAD
4. Branching
Branches are the heart of Git collaboration. They let you work on features or fixes in isolation without affecting the main codebase.
git branch
# List local branches
git branch
# List all branches including remote-tracking
git branch -a
# Create a new branch
git branch feature/login
# Delete a merged branch
git branch -d feature/login
# Force-delete an unmerged branch
git branch -D old-experiment
# Rename the current branch
git branch -m new-name
git switch (modern) / git checkout
Change your working directory to a different branch. git switch is the modern replacement for the branch-switching use of git checkout.
# Switch to an existing branch (modern)
git switch feature/login
# Create and switch to a new branch
git switch -c feature/signup
# Switch back to previous branch
git switch -
# Old style (still works)
git checkout feature/login
git checkout -b feature/signup
git merge
Combine changes from one branch into another. The most common way to integrate completed feature branches.
# Merge a branch into the current branch
git merge feature/login
# Create a merge commit even if a fast-forward is possible
git merge --no-ff feature/login
# Abort a merge in progress (if you hit conflicts)
git merge --abort
git rebase
Replay commits from one branch on top of another. Creates a cleaner, linear history — but rewrites commit hashes, so never rebase shared branches.
# Rebase current branch onto main
git rebase main
# Interactive rebase — edit, squash, reorder the last 5 commits
git rebase -i HEAD~5
# Abort a rebase in progress
git rebase --abort
# Continue after resolving conflicts
git rebase --continue
git cherry-pick
Apply a single commit from any branch onto the current branch. Useful when you need one specific fix without merging an entire branch.
# Apply a specific commit
git cherry-pick a1b2c3d
# Apply multiple commits
git cherry-pick a1b2c3d e4f5g6h
# Cherry-pick without creating a commit (stage only)
git cherry-pick -n a1b2c3d
5. Remote Operations
These commands connect your local repository to remote servers like GitHub, GitLab, or Bitbucket.
git remote
# List remote connections
git remote -v
# Add a new remote
git remote add origin https://github.com/user/repo.git
# Change the URL of an existing remote
git remote set-url origin https://github.com/user/new-repo.git
# Remove a remote
git remote remove upstream
git fetch
Download changes from the remote without merging them. Safe to run any time — it never modifies your working directory.
# Fetch all branches from origin
git fetch origin
# Fetch and prune deleted remote branches
git fetch --prune
# Fetch a specific branch
git fetch origin main
git pull
Fetch and immediately merge (or rebase) remote changes into your current branch. Equivalent to git fetch followed by git merge.
# Pull and merge (default)
git pull origin main
# Pull and rebase instead of merge (cleaner history)
git pull --rebase origin main
# Set rebase as the default pull strategy
git config --global pull.rebase true
git push
Upload your local commits to the remote repository.
# Push current branch to origin
git push origin main
# Set upstream and push (only needed once per branch)
git push -u origin feature/login
# Push all local tags
git push --tags
# Delete a remote branch
git push origin --delete old-branch
6. Undoing Mistakes
Everyone makes mistakes. Git gives you multiple ways to undo them — from lightweight (unstage a file) to powerful (rewrite history). Choose the right tool based on how far you've gone.
git restore
Discard changes to files. This is the modern replacement for git checkout -- file.
# Discard changes in the working directory
git restore index.html
# Unstage a file (keep changes in working directory)
git restore --staged index.html
# Restore a file to how it looked in a specific commit
git restore --source HEAD~2 index.html
git reset
Move the HEAD pointer (and optionally the staging area and working directory) back to a previous commit. The --soft, --mixed, and --hard flags control how far the reset goes.
# Undo the last commit but keep changes staged
git reset --soft HEAD~1
# Undo the last commit and unstage changes (default)
git reset HEAD~1
# Undo the last commit AND discard all changes (destructive!)
git reset --hard HEAD~1
# Unstage a specific file
git reset HEAD index.html
git revert
Create a new commit that undoes a previous commit. Unlike git reset, this is safe to use on shared branches because it doesn't rewrite history.
# Revert a specific commit
git revert a1b2c3d
# Revert without immediately committing
git revert -n a1b2c3d
git clean
Remove untracked files from the working directory. Be careful — there's no undo.
# Preview what would be deleted (dry run)
git clean -n
# Delete untracked files
git clean -f
# Delete untracked files and directories
git clean -fd
# Also remove ignored files
git clean -fdx
7. Stashing
Stash is a temporary shelf for work in progress. Use it when you need to switch context quickly without committing half-done work.
git stash
# Stash current changes
git stash
# Stash with a description
git stash push -m "half-done login form"
# List all stashes
git stash list
# Apply the most recent stash (keeps 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 all stashes
git stash clear
8. Tags
Tags mark specific points in history — typically used for releases.
git tag
# 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 a1b2c3d -m "Release version 1.0.0"
# Push a tag to remote
git push origin v1.0.0
# Push all tags
git push --tags
# Delete a local tag
git tag -d v1.0.0-rc
9. Advanced Commands
git reflog
The safety net. Reflog records every time HEAD moves — even after resets and rebases. If you lose commits, check here first.
# View HEAD movement history
git reflog
# Recover a lost commit
git checkout -b recovery a1b2c3d
git bisect
Find the commit that introduced a bug using binary search. Git checks out commits halfway between "good" and "bad" until it pinpoints the culprit.
# Start the bisect session
git bisect start
# Mark the current commit as bad
git bisect bad
# Mark a known-good commit
git bisect good v1.0.0
# Test the checked-out commit, then mark it:
git bisect good # or git bisect bad
# End the session
git bisect reset
git worktree
Check out multiple branches simultaneously in separate directories. Perfect for reviewing a PR while continuing work on another feature.
# Add a worktree for a branch
git worktree add ../hotfix hotfix/critical-bug
# List worktrees
git worktree list
# Remove a worktree
git worktree remove ../hotfix
git submodule
Include another Git repository inside your repository. Common for shared libraries and vendored dependencies.
# Add a submodule
git submodule add https://github.com/user/lib.git vendor/lib
# Initialize and clone all submodules after cloning a repo
git submodule update --init --recursive
# Update all submodules to their latest remote commit
git submodule update --remote
10. Useful Aliases
Git aliases let you create shortcuts for long commands. Add these to your global config to speed up your workflow:
# Compact log with graph
git config --global alias.lg "log --oneline --graph --decorate --all"
# Quick status
git config --global alias.st "status -s"
# Undo last commit (keep changes staged)
git config --global alias.undo "reset --soft HEAD~1"
# Show last commit
git config --global alias.last "log -1 HEAD --stat"
# List all aliases
git config --global alias.aliases "config --get-regexp alias"
After adding these, you can run git lg instead of the full log command, git st instead of git status, and so on.
Quick Reference: Git Command Cheatsheet
SETUP STAGING BRANCHING REMOTE
git config git status git branch git remote
git init git add git switch git fetch
git clone git commit git merge git pull
git diff git rebase git push
git rm/mv git cherry-pick
HISTORY UNDOING ADVANCED
git log git restore git stash
git show git reset git tag
git blame git revert git reflog
git shortlog git clean git bisect
Pro Tips for Working with Git
- Commit early, commit often. Small commits are easier to review, revert, and understand than giant multi-feature commits.
- Never force-push to a shared branch. Rewriting shared history breaks everyone else's clones.
-
Use
.gitignoreto exclude build artifacts, secrets, and IDE files before your first commit. - Pull before you push. Always fetch and integrate remote changes before pushing your own to avoid unnecessary merge conflicts.
- Write meaningful commit messages. Your future self (and your teammates) will thank you. Follow the convention: subject line in imperative mood, blank line, optional body with context.
- Use branches for everything. Even a five-minute fix benefits from a branch — it keeps the main branch deployable at all times.
-
Learn
git reflog. It has saved countless developers who thought they'd irreversibly lost their work.
Next Steps
Knowing these commands is a great start, but Git mastery comes from practice. Here are some ways to level up:
- Practice the entire workflow: branch → commit → push → pull request → merge.
- Use
git log --graphregularly to build an intuition for what's happening in your repo. - Try
git rebase -ito clean up commits before code review — it makes a huge impression on reviewers. - Study interactive rebase in depth — it's one of the most underused Git superpowers.
Want to take your developer tooling further? Explore our free developer tools including JSON formatters, regex testers, and more at devplaybook.cc.
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)