Git Commands Cheat Sheet: Every Developer's Essential Reference (2026)

Every Git command worth memorizing — organized by workflow with copy-paste examples. Repository setup, branching, merging, rebasing, stashing, resetting, cherry-picking, and the recovery commands that save you when things go wrong.

Table of Contents
  1. Repository Setup and Configuration
  2. Daily Basics: add, commit, push, pull
  3. Branching and Switching
  4. Merging Branches
  5. Rebasing for Clean History
  6. Stashing Work in Progress
  7. Reset, Revert, and Undo
  8. Cherry-Pick and Selective Changes
  9. Log, Diff, and Inspection
  10. Remote Repository Management
  11. Tags and Releases
  12. Advanced Workflows and Pro Tips
  13. Related Free Tools
  14. Frequently Asked Questions

Repository Setup and Configuration

Before writing any code, you need a repository. These commands handle initialization, cloning, and the global configuration that applies to every repo on your machine.

bash — Repository setup
# Create a new repository in the current directory
git init

# Create a new repository in a specific directory
git init my-project

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

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

# Shallow clone (only latest commit, faster for large repos)
git clone --depth 1 https://github.com/user/repo.git

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

Global Configuration

Set your identity and preferences once. These apply to every repository on your system unless overridden with --local inside a specific repo.

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

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

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

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

# Set pull strategy to avoid merge commits on pull
git config --global pull.rebase true

# View all settings
git config --list

# View a specific setting
git config user.email

Daily Basics: add, commit, push, pull

These are the commands you run dozens of times a day. Stage changes, commit them with a message, and synchronize with the remote repository.

bash — Core workflow
# Check what has changed
git status

# Stage a specific file
git add index.html

# Stage all changes in current directory
git add .

# Stage only modified and deleted files (not new untracked files)
git add -u

# Interactively choose hunks to stage
git add -p

# Commit with a message
git commit -m "Add user authentication"

# Stage all tracked files and commit in one step
git commit -am "Fix login validation bug"

# Amend the last commit message or add forgotten files
git add forgotten-file.js
git commit --amend -m "Add user authentication with validation"

# Push to remote
git push origin main

# Pull latest changes from remote
git pull origin main

# Fetch without merging (inspect before integrating)
git fetch origin
Pro Tip: Write Better Commit Messages

Use imperative mood: "Add feature" not "Added feature." Keep the first line under 72 characters. Add a blank line then a detailed body for complex changes. Your future self will thank you when running git log six months later.

The Git Command Builder helps assemble common Git operations without memorizing every flag.

Branching and Switching

Branches let you work on features, fixes, and experiments without affecting the main codebase. Modern Git uses git switch instead of git checkout for branch operations, though both still work.

bash — Branch operations
# List local branches (* marks the current one)
git branch

# List all branches including remote
git branch -a

# List branches with last commit info
git branch -v

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

# Switch to a branch (modern syntax)
git switch feature/user-auth

# Create and switch in one step
git switch -c feature/user-auth

# Switch to previous branch (like cd -)
git switch -

# Rename current branch
git branch -m new-name

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

# Delete a merged branch
git branch -d feature/user-auth

# Force-delete an unmerged branch
git branch -D feature/user-auth

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

Merging Branches

Merging integrates changes from one branch into another. It creates a merge commit that has two parents, preserving the history of both branches.

bash — Merge operations
# Merge a branch into current branch
git switch main
git merge feature/user-auth

# Merge with a custom commit message
git merge feature/user-auth -m "Merge user auth feature"

# Merge without fast-forward (always create a merge commit)
git merge --no-ff feature/user-auth

# Abort a merge with conflicts
git merge --abort

# View files with merge conflicts
git diff --name-only --diff-filter=U

# After resolving conflicts, complete the merge
git add .
git commit
Resolving Merge Conflicts

When Git cannot auto-merge, it marks conflicts in files with <<<<<<<, =======, and >>>>>>> markers. Edit each file to keep the changes you want, remove the markers, then git add and git commit. Use git status to see which files still need resolution.

Rebasing for Clean History

Rebase replays your commits on top of another branch, creating a linear history. It is the preferred way to update a feature branch with changes from main before merging.

bash — Rebase operations
# Rebase current branch onto main
git rebase main

# Interactive rebase: edit, squash, reorder last 5 commits
git rebase -i HEAD~5

# Continue after resolving a rebase conflict
git rebase --continue

# Skip the current conflicting commit
git rebase --skip

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

# Rebase onto a specific commit
git rebase --onto main feature-start feature-end
Golden Rule of Rebasing

Never rebase commits that have been pushed to a shared branch. Rebasing rewrites commit hashes, which forces every other developer to deal with diverged history. Only rebase local, unpushed commits or your own feature branches.

Interactive Rebase Commands

When you run git rebase -i, Git opens your editor with a list of commits. Each line starts with a command:

Stashing Work in Progress

Stash saves your uncommitted changes so you can switch context without committing half-finished work. Think of it as a clipboard for your working directory.

bash — Stash operations
# Stash all uncommitted changes
git stash

# Stash with a descriptive message
git stash push -m "WIP: user profile redesign"

# Stash including untracked files
git stash -u

# Stash including untracked and ignored files
git stash -a

# List all stashes
git stash list

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

# Apply and remove most recent stash
git stash pop

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

# View what a stash contains
git stash show -p stash@{0}

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

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

# Clear all stashes
git stash clear

Reset, Revert, and Undo

Things go wrong. These commands let you undo changes at every level — from unstaging files to rewriting commit history.

bash — Undo operations
# Unstage a file (keep changes in working directory)
git restore --staged file.js

# Discard changes to a file in working directory
git restore file.js

# Undo last commit, keep changes staged
git reset --soft HEAD~1

# Undo last commit, keep changes unstaged
git reset HEAD~1

# Undo last commit, discard all changes (DANGEROUS)
git reset --hard HEAD~1

# Reset to a specific commit
git reset --hard abc1234

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

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

# Recover a deleted branch or lost commit via reflog
git reflog
git checkout abc1234
The Reflog is Your Safety Net

git reflog records every time HEAD moves — commits, resets, rebases, checkouts. Even after a git reset --hard, you can find the lost commit hash in the reflog and recover it. The reflog keeps entries for 90 days by default.

Cherry-Pick and Selective Changes

Cherry-pick lets you copy specific commits from one branch to another without merging the entire branch.

bash — Cherry-pick operations
# Apply a specific commit to current branch
git cherry-pick abc1234

# Cherry-pick without auto-committing
git cherry-pick --no-commit abc1234

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

# Cherry-pick multiple specific commits
git cherry-pick abc1234 def5678 ghi9012

# Abort a cherry-pick with conflicts
git cherry-pick --abort

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

Log, Diff, and Inspection

Understanding what happened in a repository is just as important as making changes. These commands help you inspect history, compare changes, and find specific commits.

bash — Log and diff commands
# View commit history
git log

# Compact one-line format
git log --oneline

# Show last 10 commits
git log -10

# Graph view with branch structure
git log --oneline --graph --all --decorate

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

# Search commit messages
git log --grep="fix login"

# Show commits that changed a specific file
git log -- path/to/file.js

# Show what changed in each commit
git log -p

# View changes not yet staged
git diff

# View staged changes (what will be committed)
git diff --staged

# Compare two branches
git diff main..feature/user-auth

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

# Find which commit introduced a bug (binary search)
git bisect start
git bisect bad
git bisect good abc1234
git bisect reset

Remote Repository Management

Remotes connect your local repository to hosted versions on GitHub, GitLab, or other providers.

bash — Remote operations
# List remotes with URLs
git remote -v

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

# Add a second remote (useful for forks)
git remote add upstream https://github.com/original/repo.git

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

# Fetch all remotes
git fetch --all

# Prune stale remote-tracking branches
git fetch --prune

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

# Force-push safely (refuses if remote has new commits)
git push --force-with-lease origin feature/user-auth
Use --force-with-lease Instead of --force

--force-with-lease refuses to push if someone else has pushed to the branch since your last fetch. It is a safer alternative to --force that prevents you from accidentally overwriting a teammate's work.

Tags and Releases

Tags mark specific points in history, typically used for release versions.

bash — Tag operations
# 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 1.0.0"

# Push a specific 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

Advanced Workflows and Pro Tips

Git Worktrees

Worktrees let you check out multiple branches simultaneously in separate directories, without cloning the repo again.

bash — Worktree commands
# Create a worktree for a branch
git worktree add ../hotfix-branch hotfix/critical-bug

# List worktrees
git worktree list

# Remove a worktree
git worktree remove ../hotfix-branch

Useful Git Aliases

Save time by creating shortcuts for commands you run frequently.

bash — Aliases
git config --global alias.s "status -sb"
git config --global alias.lg "log --oneline --graph --all --decorate"
git config --global alias.amend "commit --amend --no-edit"
git config --global alias.undo "reset --soft HEAD~1"
git config --global alias.today "log --oneline --since='midnight'"

Cleaning Up

bash — Cleanup commands
# Preview untracked files that would be removed
git clean -n

# Remove untracked files
git clean -f

# Remove untracked files and directories
git clean -fd

# Remove ignored files too
git clean -fdx

# Compress and optimize the repository
git gc
git gc --prune=now

Related Free Tools

These browser-based QTool utilities complement your Git workflow — no installation required.


Frequently Asked Questions

git merge creates a new merge commit that joins two branches together, preserving the full history of both branches. git rebase replays your commits on top of another branch, creating a linear history without merge commits. Use merge for shared branches where you want to preserve the exact history of when changes were integrated. Use rebase for local feature branches before merging to keep the commit history clean and linear. Never rebase commits that have already been pushed to a shared remote branch, because it rewrites commit hashes and forces other developers to resolve conflicts.

Use git reset --soft HEAD~1 to undo the last commit while keeping all changes staged. Use git reset --mixed HEAD~1 (or just git reset HEAD~1) to undo the commit and unstage the changes, but keep them in your working directory. Use git reset --hard HEAD~1 to completely discard the commit and all changes. If the commit has already been pushed to a remote, use git revert HEAD instead, which creates a new commit that undoes the changes without rewriting history.

git stash temporarily saves your uncommitted changes (both staged and unstaged) and reverts your working directory to the last commit. This is useful when you need to switch branches but are not ready to commit your current work, when you need to pull remote changes on a dirty working directory, or when you want to test something on a clean state. Use git stash to save changes, git stash pop to restore and remove the stash, or git stash apply to restore while keeping the stash. You can have multiple stashes and list them with git stash list.

When Git cannot automatically merge changes, it marks the conflicting sections in the affected files with conflict markers: <<<<<<< HEAD (your changes), ======= (separator), and >>>>>>> branch-name (incoming changes). To resolve: open each conflicted file, decide which changes to keep (or combine both), remove the conflict markers, then run git add on each resolved file, and finally git commit to complete the merge. Use git status to see which files have conflicts. Tools like VS Code highlight conflicts and offer one-click resolution buttons. Run git merge --abort to cancel the merge entirely and return to the state before you started.

git cherry-pick applies a specific commit from one branch onto your current branch. It copies the changes introduced by that commit and creates a new commit with the same changes but a different hash. Use it when you need a specific bug fix from another branch without merging the entire branch, when a commit was made on the wrong branch and needs to be applied elsewhere, or when you want to selectively backport fixes to a release branch. The syntax is git cherry-pick <commit-hash>. You can cherry-pick multiple commits at once with git cherry-pick <hash1> <hash2> or a range with git cherry-pick A..B.

NT

QTool

We build free, browser-based developer tools. 269 tool pages for web development, DevOps, and everyday coding tasks. All client-side, no signup required.

269 Developer Tools, Zero Signup

Browse 269 indexed tool pages with no QTool account required, and inspect the source on GitHub.

Browse Free Tools Read More Articles

Related Articles

Built by Miguel

Need a custom tool or website?

From . Delivered in 24-48h. You own the code.

View Services →