Skip to main content
Toolsbase Logo

Git Commands

Searchable Git commands cheat sheet organized by category. Covers basics, branching, remote operations, and undo commands with one-click copy.

Last updated:

How to Use

Expand how to use
  1. 1

    Search for commands

    Enter a command name in the search field, or filter by category (Basic, Branching, etc.) to find the command you need.

  2. 2

    Review options and examples

    Check the command description, common options, and practical usage examples.

  3. 3

    Copy commands

    Click on an example command to copy it to your clipboard for immediate use in your terminal.

git add
Add files to the staging area

Common Options

  • .: Add all changes in current directory
  • -A: Add all changes including deletions
  • -p: Interactively select changes to add
  • -u: Add only tracked file changes

Examples

git add .
Stage all changes
git add -p
Interactively select changes to stage
git add file.txt
Stage a specific file

git commit
Record changes to the repository

Common Options

  • -m: Specify commit message
  • --amend: Modify the last commit
  • -a: Auto-stage tracked files and commit
  • --no-verify: Skip pre-commit hooks

Examples

git commit -m "feat: add new feature"
Commit with a message
git commit --amend
Amend the last commit
git commit -am "fix: bug fix"
Auto-stage and commit

git status
Show working tree status

Common Options

  • -s: Short format output
  • -b: Show branch info
  • --porcelain: Machine-readable output

Examples

git status
Show current status
git status -sb
Short format with branch info

git diff
Show changes between commits

Common Options

  • --staged: Show staged changes
  • --cached: Same as --staged
  • --stat: Show diffstat
  • --name-only: Show only file names

Examples

git diff
Show unstaged changes
git diff --staged
Show staged changes
git diff HEAD~1
Compare with previous commit

git log
Show commit history

Common Options

  • --oneline: One line per commit
  • --graph: Show branch graph
  • -n: Show last n commits
  • -p: Show patches

Examples

git log --oneline
Show compact history
git log --oneline --graph
Show history with graph
git log -5
Show last 5 commits

git push
Update remote refs

Common Options

  • -u: Set upstream branch
  • --force: Force push (caution)
  • --force-with-lease: Safer force push
  • --tags: Push tags as well

Examples

git push origin main
Push main to origin
git push -u origin feature
Push and set upstream
git push --force-with-lease
Safe force push

git pull
Fetch and integrate remote changes

Common Options

  • --rebase: Rebase instead of merge
  • --no-rebase: Merge (default)
  • --ff-only: Fast-forward only

Examples

git pull
Fetch and merge
git pull --rebase
Fetch and rebase
git pull origin main
Pull from origin main

git fetch
Download objects from remote (no merge)

Common Options

  • --all: Fetch from all remotes
  • --prune: Remove deleted remote branches
  • --tags: Fetch tags

Examples

git fetch origin
Fetch from origin
git fetch --all
Fetch from all remotes
git fetch --prune
Clean up deleted branches

git branch
List, create, or delete branches

Common Options

  • -a: List all branches
  • -d: Delete branch
  • -D: Force delete
  • -m: Rename branch

Examples

git branch
List local branches
git branch feature/new
Create new branch
git branch -d feature/old
Delete merged branch

git checkout
Switch branches or restore files (legacy)

Common Options

  • -b: Create and switch to new branch
  • --: Restore file
  • -B: Force create/reset branch

Examples

git checkout main
Switch to main branch
git checkout -b feature/new
Create and switch branch
git checkout -- file.txt
Restore file to last commit

git switch
Switch branches (Git 2.23+)

Common Options

  • -c: Create and switch to new branch
  • -C: Reset existing branch and switch
  • -d: Detached HEAD checkout

Examples

git switch main
Switch to main branch
git switch -c feature/new
Create and switch branch
git switch -
Switch to previous branch

git merge
Join development histories

Common Options

  • --no-ff: Create merge commit
  • --squash: Squash commits
  • --abort: Abort merge

Examples

git merge feature/new
Merge feature/new
git merge --no-ff feature/new
Merge with commit
git merge --abort
Abort conflicted merge

git rebase
Reapply commits on top of another base

Common Options

  • -i: Interactive mode
  • --onto: Specify new base
  • --continue: Continue after conflict
  • --abort: Abort rebase

Examples

git rebase main
Rebase onto main
git rebase -i HEAD~3
Edit last 3 commits
git rebase --abort
Abort and restore

git show
Show commit details

Common Options

  • --stat: Show file change stats
  • --name-only: Show only file names
  • --oneline: One line format

Examples

git show
Show latest commit
git show HEAD~1
Show previous commit
git show abc1234
Show specific commit

git blame
Show who changed each line

Common Options

  • -L: Specify line range
  • -w: Ignore whitespace
  • -C: Detect copied code

Examples

git blame file.txt
Show line-by-line authors
git blame -L 10,20 file.txt
Blame lines 10-20
git blame -w file.txt
Ignore whitespace changes

git reflog
Show HEAD movement history

Common Options

  • --all: Show all refs
  • -n: Show last n entries
  • --date=relative: Relative dates

Examples

git reflog
Show HEAD history
git reflog -10
Show last 10 entries
git checkout HEAD@{2}
Restore previous state

git shortlog
Summarize commits by author

Common Options

  • -s: Show commit count only
  • -n: Sort by commit count
  • -e: Show email addresses

Examples

git shortlog -sn
Show contributors by count
git shortlog -sne
Show with emails
git shortlog HEAD~100..HEAD
Last 100 commits contributors

git reset
Reset HEAD to specified state

Common Options

  • --soft: Keep changes staged
  • --mixed: Unstage changes (default)
  • --hard: Discard all changes (caution)

Examples

git reset --soft HEAD~1
Undo commit, keep changes
git reset HEAD file.txt
Unstage a file
git reset --hard HEAD~1
Discard last commit

git revert
Create commit that undoes changes

Common Options

  • -n: Don't commit, just apply
  • --no-edit: Skip editor
  • -m: Specify parent for merge

Examples

git revert HEAD
Revert last commit
git revert abc1234
Revert specific commit
git revert -n HEAD~3..HEAD
Revert multiple commits

git restore
Restore files (Git 2.23+)

Common Options

  • --staged: Unstage file
  • --source: Restore from commit
  • --worktree: Restore working tree (default)

Examples

git restore file.txt
Restore file to last commit
git restore --staged file.txt
Unstage file
git restore --source HEAD~1 file.txt
Restore from previous commit

git clean
Remove untracked files

Common Options

  • -n: Dry run (preview)
  • -f: Force deletion
  • -d: Remove directories too
  • -x: Remove ignored files too

Examples

git clean -n
Preview what will be deleted
git clean -fd
Remove untracked files and dirs
git clean -fdx
Remove including ignored files

git stash
Temporarily save changes

Common Options

  • push: Save to stash
  • pop: Apply and remove from stash
  • list: List stashes
  • drop: Remove stash

Examples

git stash
Save changes temporarily
git stash pop
Restore saved changes
git stash list
List all stashes

git remote
Manage remote repositories

Common Options

  • -v: Show URLs
  • add: Add remote
  • remove: Remove remote
  • rename: Rename remote

Examples

git remote -v
List remotes with URLs
git remote add upstream https://github.com/user/repo.git
Add upstream remote
git remote rename origin old-origin
Rename remote

git config
Get and set Git options

Common Options

  • --global: User-wide settings
  • --local: Repository settings
  • --list: List all settings
  • --unset: Remove setting

Examples

git config --global user.name "Your Name"
Set user name
git config --global user.email "you@example.com"
Set email address
git config --list
List all settings

git init
Create empty Git repository

Common Options

  • --bare: Create bare repository
  • -b: Set initial branch name
  • --initial-branch: Set initial branch name

Examples

git init
Initialize current directory
git init my-project
Create new repo in directory
git init -b main
Initialize with main branch

git clone
Clone repository into new directory

Common Options

  • --depth: Shallow clone
  • -b: Clone specific branch
  • --single-branch: Clone only one branch
  • --recurse-submodules: Include submodules

Examples

git clone https://github.com/user/repo.git
Clone a repository
git clone --depth 1 https://github.com/user/repo.git
Shallow clone
git clone -b develop https://github.com/user/repo.git
Clone develop branch

git tag
Create, list, or delete tags

Common Options

  • -a: Create annotated tag
  • -m: Tag message
  • -d: Delete tag
  • -l: List tags

Examples

git tag v1.0.0
Create lightweight tag
git tag -a v1.0.0 -m "Release 1.0.0"
Create annotated tag
git tag -d v1.0.0
Delete tag

git cherry-pick
Apply specific commits to current branch

Common Options

  • -n: Apply without committing
  • --no-commit: Same as -n
  • -x: Append commit ID to message
  • --abort: Abort cherry-pick

Examples

git cherry-pick abc1234
Apply specific commit
git cherry-pick abc1234 def5678
Apply multiple commits
git cherry-pick --abort
Abort cherry-pick

git bisect
Binary search to find bug-introducing commit

Common Options

  • start: Start bisect
  • good: Mark commit as good
  • bad: Mark commit as bad
  • reset: End bisect

Examples

git bisect start
Start binary search
git bisect bad HEAD
Mark HEAD as bad
git bisect good v1.0.0
Mark v1.0.0 as good

git grep
Search text in repository

Common Options

  • -n: Show line numbers
  • -i: Case insensitive
  • -l: Show only file names
  • -c: Count matches

Examples

git grep "TODO"
Search for TODO
git grep -n "function"
Search with line numbers
git grep -l "import"
Show matching file names

About Git Commands

Git Commands is a cheat sheet compiling frequently used Git commands. It comprehensively covers commands needed for development, from basic operations to branching, history viewing, and undo operations. Each command includes descriptions, common options, and practical examples that can be copied with one click.

Key Features

  • Collection of 30+ essential Git commands
  • Options and usage examples for each command
  • Category filtering (basic, branching, history, etc.)
  • Real-time search functionality
  • One-click copy of examples

Use Cases

  • Looking up the correct syntax for git rebase, cherry-pick, or bisect
  • Quickly checking git branch, merge, and stash options during terminal work
  • Learning Git as a beginner moving from GitHub Desktop to the command line
  • Referencing undo commands (reset, revert, restore) when you need to roll back a change
  • Using as a cheat sheet during code review, pair programming, or a GitHub PR workflow
  • Onboarding new team members to your team's Git workflow and branching conventions

FAQ

What's the difference between git pull and git fetch?

git fetch only downloads remote changes to local without affecting your working branch. git pull performs a fetch and then automatically merges or rebases to incorporate changes into your working branch.

What's the difference between git reset and git revert?

git reset rewrites commit history to return to a previous state. git revert creates a new commit that undoes changes, preserving history. revert is safer for shared branches.

What's the difference between checkout and switch?

switch was added in Git 2.23 specifically for branch switching. checkout handles both branch switching and file restoration, mixing responsibilities. Using the newer commands (switch/restore) is recommended.

What's the difference between --force and --force-with-lease?

--force pushes unconditionally, while --force-with-lease only pushes if the remote is in the expected state. This reduces the risk of accidentally overwriting others' changes.

What if I accidentally deleted a commit?

Use git reflog to view the history of HEAD movements. You can find the hash of the deleted commit and potentially recover it using git checkout or git cherry-pick.