Skip to content
back to cheatsheets

Git Commands Cheatsheet — Essential Commands by Workflow

· Reference

Git is the standard version control system for software development. This reference organizes the commands you actually use day-to-day, grouped by workflow stage. Each command includes the flags and options that matter most.

Setup and Configuration

# Set your identity
git config --global user.name "Your Name"
git config --global user.email "you@example.com"

# Set default branch name
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 settings
git config --list --show-origin

# Initialize a new repository
git init

# Clone a repository
git clone <url>
git clone <url> <directory>          # Clone into specific directory
git clone --depth 1 <url>            # Shallow clone (latest commit only)

Staging and Committing

# Check status
git status
git status -s                         # Short format

# Stage files
git add <file>                        # Stage a specific file
git add .                             # Stage all changes in current directory
git add -p                            # Interactively stage hunks

# Unstage files
git restore --staged <file>           # Unstage (keep changes)
git reset HEAD <file>                 # Legacy alternative

# Commit
git commit -m "message"               # Commit with message
git commit -am "message"              # Stage tracked files and commit
git commit --amend                    # Modify the last commit
git commit --amend --no-edit          # Amend without changing message
git commit --allow-empty -m "msg"     # Commit with no file changes

# View changes
git diff                              # Unstaged changes
git diff --staged                     # Staged changes
git diff <branch1>..<branch2>         # Between branches
git diff --stat                       # Summary of changes

Branching

# List branches
git branch                            # Local branches
git branch -r                         # Remote branches
git branch -a                         # All branches
git branch -v                         # With last commit info

# Create branches
git branch <name>                     # Create branch
git checkout -b <name>                # Create and switch (legacy)
git switch -c <name>                  # Create and switch (modern)

# Switch branches
git checkout <branch>                 # Legacy
git switch <branch>                   # Modern

# Rename branch
git branch -m <old> <new>             # Rename
git branch -m <new>                   # Rename current branch

# Delete branches
git branch -d <name>                  # Delete (safe — only if merged)
git branch -D <name>                  # Force delete
git push origin --delete <name>       # Delete remote branch

Merging and Rebasing

# Merge
git merge <branch>                    # Merge branch into current
git merge --no-ff <branch>            # Force a merge commit
git merge --squash <branch>           # Squash all commits into one
git merge --abort                     # Abort an in-progress merge

# Rebase
git rebase <branch>                   # Rebase current onto branch
git rebase -i HEAD~3                  # Interactive rebase last 3 commits
git rebase --onto <new> <old> <br>    # Transplant branch
git rebase --abort                    # Abort rebase
git rebase --continue                 # Continue after resolving conflicts

# Cherry-pick
git cherry-pick <commit>              # Apply a specific commit
git cherry-pick <c1>..<c2>            # Apply a range
git cherry-pick --no-commit <commit>  # Apply changes without committing

Remote Repositories

# Manage remotes
git remote -v                         # List remotes with URLs
git remote add <name> <url>           # Add a remote
git remote remove <name>              # Remove a remote
git remote rename <old> <new>         # Rename a remote
git remote set-url <name> <url>       # Change remote URL

# Fetch and pull
git fetch                             # Download from all remotes
git fetch <remote>                    # Download from specific remote
git fetch --prune                     # Remove deleted remote branches
git pull                              # Fetch and merge
git pull --rebase                     # Fetch and rebase

# Push
git push                              # Push current branch
git push -u origin <branch>           # Push and set upstream
git push --force-with-lease           # Safe force push
git push --tags                       # Push all tags

Stashing

# Save work in progress
git stash                             # Stash tracked changes
git stash -u                          # Include untracked files
git stash push -m "description"       # Stash with a name
git stash push <file>                 # Stash specific file

# Retrieve stashed work
git stash pop                         # Apply and remove latest stash
git stash apply                       # Apply but keep in stash list
git stash apply stash@{2}             # Apply specific stash

# Manage stashes
git stash list                        # List all stashes
git stash show                        # Show stash summary
git stash show -p                     # Show stash diff
git stash drop                        # Remove latest stash
git stash clear                       # Remove all stashes

Log and History

# View history
git log                               # Full log
git log --oneline                     # Compact format
git log --graph --oneline --all       # Visual branch graph
git log -n 5                          # Last 5 commits
git log --author="name"               # Filter by author
git log --since="2 weeks ago"         # Filter by date
git log --grep="fix"                  # Search commit messages
git log -p <file>                     # History of a file with diffs
git log --stat                        # Show files changed per commit

# Inspect
git show <commit>                     # Show commit details
git blame <file>                      # Who changed each line
git shortlog -sn                      # Commit count per author
git log --all --oneline --graph       # ASCII branch visualization

Undoing Changes

# Discard working directory changes
git restore <file>                    # Discard changes (modern)
git checkout -- <file>                # Discard changes (legacy)

# Undo commits
git revert <commit>                   # Create a new commit that undoes changes
git reset --soft HEAD~1               # Undo commit, keep changes staged
git reset --mixed HEAD~1              # Undo commit, keep changes unstaged
git reset --hard HEAD~1               # Undo commit and discard changes

# Recover
git reflog                            # View history of HEAD changes
git checkout <reflog-hash>            # Restore to a reflog state

Tags

# Create tags
git tag <name>                        # Lightweight tag
git tag -a <name> -m "message"        # Annotated tag
git tag -a <name> <commit>            # Tag a specific commit

# List and inspect
git tag                               # List all tags
git tag -l "v1.*"                     # Filter tags by pattern
git show <tag>                        # Show tag details

# Share tags
git push origin <tag>                 # Push a specific tag
git push --tags                       # Push all tags
git push origin --delete <tag>        # Delete remote tag
git tag -d <tag>                      # Delete local tag

Useful Aliases

Add these to your ~/.gitconfig under [alias]:

[alias]
  st = status -s
  co = checkout
  br = branch
  ci = commit
  lg = log --oneline --graph --all --decorate
  unstage = restore --staged
  last = log -1 HEAD
  undo = reset --soft HEAD~1

Browse the full reference interactively with the Git Cheatsheet tool or generate .gitignore files with the Gitignore Generator.

#Learn More