ToolBox Hub

Git Commands Cheat Sheet: 50+ Essential Commands Every Developer Needs

Git Commands Cheat Sheet: 50+ Essential Commands Every Developer Needs

The ultimate Git cheat sheet with 50+ essential commands explained with examples. From basic operations to advanced workflows, branching strategies, and troubleshooting tips for 2026.

March 15, 202618 min read

Introduction: Why Every Developer Needs Git Mastery

Git is the backbone of modern software development. Whether you are working solo on a personal project or collaborating with hundreds of developers across multiple teams, Git is the tool that keeps everything organized, trackable, and reversible. Despite being created in 2005 by Linus Torvalds, Git remains the undisputed standard for version control in 2026.

Yet many developers only know a handful of Git commands. They clone, commit, push, pull, and occasionally Google how to undo something. This cheat sheet aims to change that. We will cover over 50 essential Git commands organized by category, with practical examples and explanations of when and why to use each one.

Whether you are a beginner learning Git for the first time or an experienced developer looking for a quick reference, bookmark this page. You will come back to it often.

Getting Started: Configuration and Setup

Before using Git, you need to configure it. These commands set up your identity and preferences.

git config

Configure Git settings at system, global, or repository level.

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

# Set default branch name to main
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

# View all configuration
git config --list

# View specific setting
git config user.name

git init

Create a new Git repository in the current directory.

# Initialize a new repository
git init

# Initialize with a specific default branch name
git init --initial-branch=main

# Initialize a bare repository (for remote servers)
git init --bare

git clone

Copy an existing repository to your local machine.

# Clone via HTTPS
git clone https://github.com/user/repository.git

# Clone via SSH (recommended for regular contributors)
git clone git@github.com:user/repository.git

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

# Clone only a specific branch
git clone --branch develop --single-branch https://github.com/user/repository.git

# Shallow clone (only recent history, faster for large repos)
git clone --depth 1 https://github.com/user/repository.git

Daily Workflow: The Commands You Use Every Day

These are the commands that form the core of your daily Git workflow.

git status

Check the state of your working directory and staging area.

# Full status output
git status

# Short format (more compact)
git status -s

# Show branch info in short format
git status -sb

Status codes in short format:

  • ?? -- Untracked file
  • A -- New file added to staging
  • M -- Modified file
  • D -- Deleted file
  • R -- Renamed file

git add

Stage changes for the next commit.

# Stage a specific file
git add filename.js

# Stage multiple specific files
git add file1.js file2.js file3.js

# Stage all changes in current directory
git add .

# Stage all changes in entire repository
git add -A

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

# Stage all modified files (not new untracked files)
git add -u

Pro tip: Use git add -p to review and stage individual hunks of changes. This is invaluable for creating clean, focused commits.

git commit

Record staged changes in the repository history.

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

# Commit with a multi-line message
git commit -m "Add user authentication feature" -m "Implements JWT-based auth with refresh tokens"

# Stage all modified files and commit in one step
git commit -am "Fix navigation bug"

# Amend the last commit (change message or add files)
git commit --amend -m "Updated commit message"

# Amend without changing the message
git commit --amend --no-edit

# Create an empty commit (useful for triggering CI)
git commit --allow-empty -m "Trigger build"

Writing Good Commit Messages

Good commit messages follow these conventions:

type(scope): subject

body (optional)

footer (optional)

Types: feat, fix, docs, style, refactor, test, chore

Examples of good commit messages:

feat(auth): add JWT refresh token rotation
fix(api): handle null response in user endpoint
docs(readme): update installation instructions
refactor(utils): extract date formatting to shared module

git push

Upload local commits to a remote repository.

# Push to the default remote (origin) and branch
git push

# Push and set upstream tracking
git push -u origin main

# Push a specific branch
git push origin feature/login

# Push all branches
git push --all

# Push tags
git push --tags

# Force push (use with caution!)
git push --force-with-lease

Important: Always prefer --force-with-lease over --force. It prevents overwriting others' work by checking that the remote has not been updated since your last fetch.

git pull

Fetch and integrate changes from a remote repository.

# Pull from the tracked remote branch
git pull

# Pull with rebase instead of merge
git pull --rebase

# Pull from a specific remote and branch
git pull origin main

# Pull and automatically stash/unstash local changes
git pull --autostash

git fetch

Download objects and refs from a remote without merging.

# Fetch from default remote
git fetch

# Fetch from a specific remote
git fetch origin

# Fetch all remotes
git fetch --all

# Fetch and prune deleted remote branches
git fetch --prune

fetch vs. pull: git fetch downloads changes but does not modify your working directory. git pull is essentially git fetch + git merge. Use fetch when you want to review changes before integrating them.

Branching and Merging

Branching is one of Git's most powerful features. It lets you work on features, fixes, and experiments in isolation.

git branch

List, create, or delete branches.

# List local branches
git branch

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

# List branches with last commit info
git branch -v

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

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

# Force delete a branch
git branch -D feature/old-feature

# Rename current branch
git branch -m new-name

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

# Show branches that have been merged into current branch
git branch --merged

# Show branches not yet merged
git branch --no-merged

git checkout / git switch

Switch between branches or restore files.

# Switch to an existing branch (modern way)
git switch main

# Create and switch to a new branch (modern way)
git switch -c feature/new-feature

# Switch to an existing branch (classic way)
git checkout main

# Create and switch to a new branch (classic way)
git checkout -b feature/new-feature

# Restore a file to its last committed state
git restore filename.js

# Restore a file from a specific commit
git restore --source=HEAD~2 filename.js

# Unstage a file
git restore --staged filename.js

Note: git switch and git restore were introduced in Git 2.23 to replace the overloaded git checkout command. They are clearer about intent.

git merge

Combine branches together.

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

# Merge with a commit message
git merge feature/new-feature -m "Merge feature/new-feature into main"

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

# Abort a merge in progress
git merge --abort

# Continue merge after resolving conflicts
git merge --continue

git rebase

Reapply commits on top of another base.

# Rebase current branch onto main
git rebase main

# Interactive rebase (edit, squash, reorder commits)
git rebase -i HEAD~5

# Abort a rebase in progress
git rebase --abort

# Continue rebase after resolving conflicts
git rebase --continue

# Skip the current commit during rebase
git rebase --skip

Interactive rebase commands:

  • pick -- Keep the commit as-is
  • reword -- Keep the commit but change the message
  • squash -- Combine with the previous commit
  • fixup -- Like squash but discard the commit message
  • drop -- Remove the commit entirely
  • edit -- Pause to amend the commit

git cherry-pick

Apply specific commits from one branch to another.

# Apply a specific commit
git cherry-pick abc1234

# Cherry-pick without committing (just stage the changes)
git cherry-pick --no-commit abc1234

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

Viewing History and Changes

Understanding what happened in your repository is crucial for debugging and code review.

git log

View commit history.

# Basic log
git log

# One line per commit
git log --oneline

# Show a graph of branches
git log --oneline --graph --all

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

# Show commits in a date range
git log --since="2026-01-01" --until="2026-03-01"

# Show commits that changed a specific file
git log -- filename.js

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

# Show the last 10 commits
git log -10

# Custom format
git log --pretty=format:"%h %an %ar - %s"

Useful git log format placeholders:

  • %h -- Abbreviated commit hash
  • %an -- Author name
  • %ar -- Author date (relative)
  • %s -- Commit subject
  • %d -- Branch/tag decorations

git diff

Show changes between commits, branches, or the working directory.

# Show unstaged changes
git diff

# Show staged changes
git diff --staged

# Show changes between two branches
git diff main..feature/login

# Show changes in a specific file
git diff filename.js

# Show only file names that changed
git diff --name-only

# Show statistics (files changed, insertions, deletions)
git diff --stat

# Compare with a specific commit
git diff HEAD~3

# Show word-level diff (useful for prose)
git diff --word-diff

git show

Display information about a Git object.

# Show the last commit
git show

# Show a specific commit
git show abc1234

# Show a file from a specific commit
git show HEAD~2:filename.js

# Show only the file names in a commit
git show --name-only abc1234

# Show tag information
git show v1.0.0

git blame

Show who last modified each line of a file.

# Show blame for entire file
git blame filename.js

# Show blame for specific lines
git blame -L 10,20 filename.js

# Show blame with email addresses
git blame -e filename.js

# Ignore whitespace changes
git blame -w filename.js

Undoing Things: The Safety Net

One of Git's greatest strengths is the ability to undo almost anything. Here are the essential undo commands.

git restore

Restore files in the working tree.

# Discard changes in a file
git restore filename.js

# Discard all changes in working directory
git restore .

# Unstage a file (keep changes in working directory)
git restore --staged filename.js

# Restore from a specific commit
git restore --source=HEAD~2 filename.js

git reset

Reset current HEAD to a specified state.

# Unstage all files (keep changes)
git reset

# Unstage a specific file
git reset HEAD filename.js

# Soft reset: move HEAD, keep staged changes
git reset --soft HEAD~1

# Mixed reset (default): move HEAD, unstage changes, keep working directory
git reset HEAD~1

# Hard reset: move HEAD, discard all changes (DANGEROUS!)
git reset --hard HEAD~1

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

Understanding reset modes:

  • --soft -- Only moves HEAD pointer. Staged area and working directory unchanged.
  • --mixed (default) -- Moves HEAD and unstages. Working directory unchanged.
  • --hard -- Moves HEAD, unstages, and discards working directory changes. Irreversible!

git revert

Create a new commit that undoes a previous commit. This is safer than reset for shared branches.

# Revert the last commit
git revert HEAD

# Revert a specific commit
git revert abc1234

# Revert without creating a commit immediately
git revert --no-commit abc1234

# Revert a merge commit
git revert -m 1 abc1234

git stash

Temporarily save changes that are not ready to commit.

# Stash current changes
git stash

# Stash with a descriptive message
git stash push -m "Work in progress: login form"

# Stash including untracked files
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}

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

# Clear all stashes
git stash clear

# Show stash contents
git stash show -p stash@{0}

Remote Repositories

git remote

Manage remote repository connections.

# List remotes
git remote -v

# Add a new remote
git remote add upstream https://github.com/original/repository.git

# Remove a remote
git remote remove upstream

# Rename a remote
git remote rename origin github

# Show detailed info about a remote
git remote show origin

# Update remote URLs
git remote set-url origin git@github.com:user/repository.git

Tags and Releases

git tag

Create, list, and manage tags.

# List all tags
git tag

# List tags matching a pattern
git tag -l "v1.*"

# Create a lightweight tag
git tag v1.0.0

# Create an annotated tag (recommended)
git tag -a v1.0.0 -m "Release version 1.0.0"

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

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

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

Advanced Operations

git worktree

Manage multiple working trees for the same repository.

# Create a new worktree for a branch
git worktree add ../feature-branch feature/login

# List all worktrees
git worktree list

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

# Prune stale worktree information
git worktree prune

git bisect

Use binary search to find the commit that introduced a bug.

# Start bisecting
git bisect start

# Mark the current commit as bad
git bisect bad

# Mark a known good commit
git bisect good abc1234

# After testing each commit Git checks out:
git bisect good  # or git bisect bad

# End bisection
git bisect reset

# Automate bisection with a test script
git bisect run npm test

git reflog

View a log of all reference updates (your safety net).

# Show reflog for HEAD
git reflog

# Show reflog for a specific branch
git reflog show main

# Recover a commit after accidental reset
git reflog
# Find the commit hash you need, then:
git reset --hard abc1234

The reflog is your last resort. If you accidentally delete a branch, reset too far, or lose commits, git reflog can help you find and recover them.

git clean

Remove untracked files from the working directory.

# Preview what would be deleted (dry run)
git clean -n

# Remove untracked files
git clean -f

# Remove untracked files and directories
git clean -fd

# Remove untracked and ignored files
git clean -fdx

git submodule

Manage Git repositories nested within your repository.

# Add a submodule
git submodule add https://github.com/user/library.git libs/library

# Initialize submodules after cloning
git submodule init
git submodule update

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

# Update all submodules to latest
git submodule update --remote

Useful Aliases

Speed up your workflow by setting up Git aliases:

# Common aliases
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status
git config --global alias.sw switch

# Useful custom aliases
git config --global alias.lg "log --oneline --graph --all --decorate"
git config --global alias.last "log -1 HEAD --stat"
git config --global alias.unstage "restore --staged"
git config --global alias.undo "reset HEAD~1 --mixed"
git config --global alias.amend "commit --amend --no-edit"

After setting these up, you can use git co main instead of git checkout main, git lg for a beautiful branch graph, and more.

Common Workflows

Feature Branch Workflow

# 1. Start from main
git switch main
git pull

# 2. Create feature branch
git switch -c feature/user-profile

# 3. Make changes and commit
git add .
git commit -m "feat(profile): add user avatar upload"

# 4. Keep up to date with main
git fetch origin
git rebase origin/main

# 5. Push and create PR
git push -u origin feature/user-profile
# Create pull request on GitHub/GitLab

# 6. After PR is merged, clean up
git switch main
git pull
git branch -d feature/user-profile

Hotfix Workflow

# 1. Create hotfix branch from main
git switch main
git pull
git switch -c hotfix/critical-bug

# 2. Fix the bug
git add .
git commit -m "fix(auth): prevent session hijacking via token reuse"

# 3. Push and create PR
git push -u origin hotfix/critical-bug

# 4. After merge, tag the release
git switch main
git pull
git tag -a v1.2.1 -m "Hotfix: prevent session hijacking"
git push --tags

Squash Commits Before Merge

# Squash the last 5 commits into one
git rebase -i HEAD~5
# Mark all but the first as "squash" or "fixup"
# Edit the combined commit message

# Alternative: squash merge
git switch main
git merge --squash feature/messy-history
git commit -m "feat(search): add full-text search functionality"

Troubleshooting Common Issues

"Detached HEAD" State

# You are in detached HEAD if you checked out a commit directly
# To fix, create a branch from current position:
git switch -c my-branch

# Or go back to a branch:
git switch main

Merge Conflicts

# When you see merge conflicts:
# 1. Open the conflicted files and look for conflict markers:
#    <<<<<<< HEAD
#    (your changes)
#    =======
#    (incoming changes)
#    >>>>>>> feature-branch

# 2. Edit the file to resolve the conflict
# 3. Stage the resolved file
git add resolved-file.js

# 4. Continue the merge/rebase
git merge --continue
# or
git rebase --continue

Accidentally Committed to Wrong Branch

# Move the last commit to a new branch
git branch correct-branch
git reset --hard HEAD~1
git switch correct-branch

Remove a File from Git Without Deleting It

# Stop tracking a file but keep it locally
git rm --cached filename.js
echo "filename.js" >> .gitignore
git commit -m "chore: stop tracking filename.js"

Find Which Commit Broke Something

# Use bisect to find the problematic commit
git bisect start
git bisect bad HEAD
git bisect good v1.0.0
# Git will checkout commits for you to test
# After testing each:
git bisect good  # or git bisect bad
# Git will identify the first bad commit
git bisect reset

Git Best Practices for 2026

  1. Commit often, push regularly -- Small, frequent commits are easier to review and revert
  2. Write meaningful commit messages -- Follow conventional commits format
  3. Use branches for everything -- Never commit directly to main
  4. Rebase before merging -- Keeps history clean and linear
  5. Use .gitignore from the start -- Prevent committing generated files, secrets, and dependencies
  6. Review diffs before committing -- Use git diff --staged to verify what you are about to commit
  7. Never force-push to shared branches -- Use --force-with-lease if you must
  8. Tag your releases -- Semantic versioning (v1.2.3) makes releases traceable
  9. Use SSH keys -- More secure and convenient than HTTPS passwords
  10. Learn to use reflog -- It is your safety net when things go wrong

Quick Reference Table

TaskCommand
Initialize repogit init
Clone repogit clone <url>
Check statusgit status
Stage changesgit add .
Commitgit commit -m "message"
Pushgit push
Pullgit pull
Create branchgit switch -c <name>
Switch branchgit switch <name>
Merge branchgit merge <name>
View loggit log --oneline
See changesgit diff
Stash changesgit stash
Undo last commitgit reset --soft HEAD~1
Discard changesgit restore <file>
Tag a releasegit tag -a v1.0 -m "msg"

Conclusion

Git is a tool you will use every single day as a developer. Mastering these commands will make you faster, more confident, and better equipped to handle any version control challenge that comes your way.

Bookmark this cheat sheet and refer to it whenever you need a quick reminder. The more you practice these commands, the more natural they will become.

For your other everyday developer needs, check out our free online tools -- from JSON formatting and Base64 encoding to regex testing and hash generation.

Related Posts