Back to Blog
February 1, 202620 min read

Git Commands Cheat Sheet: Most Useful Commands, Tips & Tricks

Master Git with this comprehensive cheat sheet. Learn the most useful Git commands, productivity tips, advanced tricks, and shortcuts that will make you a Git power user. Perfect reference for daily development.

Essential Git Commands

Repository Setup

git init

Initialize a new Git repository

Creates a .git directory in current folder

git clone <url>

Clone a remote repository

Downloads entire repository with history

git remote add origin <url>

Add remote repository

Links local repo to remote (GitHub, GitLab)

git remote -v

List remote repositories

Shows all configured remotes

Basic Workflow

git status

Check repository status

Shows modified, staged, and untracked files

git add <file>

Stage a file for commit

Add specific file to staging area

git add .

Stage all changes

Stages all modified and new files

git commit -m "message"

Create a commit

Saves staged changes with a message

git push origin <branch>

Upload commits to remote

Pushes local commits to remote branch

git pull origin <branch>

Download and merge changes

Fetches and merges remote changes

Branching

git branch

List all branches

Shows local branches (* = current branch)

git branch <name>

Create a new branch

Creates branch but doesn't switch to it

git checkout <branch>

Switch to a branch

Changes working directory to specified branch

git checkout -b <name>

Create and switch to branch

Combines branch creation and checkout

git branch -d <branch>

Delete a branch

Deletes branch (only if merged)

git branch -D <branch>

Force delete branch

Deletes branch even if not merged

git merge <branch>

Merge branch into current

Combines changes from another branch

Viewing History

git log

View commit history

Shows commits in reverse chronological order

git log --oneline

Compact commit history

One line per commit

git log --graph

Visual branch history

Shows branch structure graphically

git log --all --graph --oneline

Complete visual history

Best view of all branches

git show <commit>

Show commit details

Displays commit message and changes

git diff

Show unstaged changes

Compares working directory with staging area

git diff --staged

Show staged changes

Compares staging area with last commit

git diff HEAD

Show all changes

All changes since last commit

Undoing Changes

git restore <file>

Discard file changes

Reverts file to last committed state

git restore --staged <file>

Unstage a file

Removes file from staging area

git reset --soft HEAD~1

Undo last commit (keep changes)

Removes commit, keeps changes staged

git reset --hard HEAD~1

Undo last commit (discard changes)

Removes commit and all changes

git revert <commit>

Create undo commit

Safe way to undo pushed commits

git clean -fd

Remove untracked files

Deletes untracked files and directories

Stashing

git stash

Save changes temporarily

Stashes uncommitted changes

git stash save "message"

Stash with message

Adds description to stash

git stash list

List all stashes

Shows all saved stashes

git stash pop

Apply and remove stash

Restores most recent stash

git stash apply

Apply stash (keep it)

Restores stash but keeps it in list

git stash drop

Delete a stash

Removes stash from list

git stash clear

Delete all stashes

Removes all saved stashes

Remote Operations

git fetch

Download changes (no merge)

Updates remote tracking branches

git fetch origin

Fetch from specific remote

Downloads from origin remote

git pull --rebase

Pull with rebase

Rebases local commits on top of remote

git push -u origin <branch>

Push and set upstream

Sets tracking branch for future pulls

git push --force

Force push (dangerous)

Overwrites remote (use with caution!)

git remote show origin

Show remote details

Displays remote repository information

Advanced

git rebase <branch>

Rebase current branch

Replays commits on top of another branch

git rebase -i HEAD~3

Interactive rebase

Edit, reorder, or squash commits

git cherry-pick <commit>

Apply specific commit

Copies commit from another branch

git reflog

View reference log

Shows all HEAD movements (recovery tool)

git bisect

Binary search for bugs

Finds commit that introduced a bug

git blame <file>

Show file line authors

Shows who last modified each line

Tips & Tricks

Useful Git Aliases

Productivity
Create shortcuts for common commands:
git config --global alias.st status
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.unstage 'reset HEAD --'
git config --global alias.last 'log -1 HEAD'
git config --global alias.visual '!gitk'

Pretty Git Log

Productivity
Create a beautiful, compact log view:
git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --all"

Then use: git lg

Quick Commit All

Productivity
Stage and commit all changes in one command:
git add -A && git commit -m "message"

Or create an alias:
git config --global alias.ac '!git add -A && git commit'

Find Lost Commits

Recovery
If you accidentally deleted a branch or reset, use reflog to find it:
git reflog
git checkout <commit-hash>

Reflog keeps history of all HEAD movements for 90 days.

Amend Last Commit

Workflow
Add changes to the last commit:
git add <file>
git commit --amend --no-edit

Or change the commit message:
git commit --amend -m "New message"

Stash with Untracked Files

Stashing
By default, stash only saves tracked files. Include untracked:
git stash -u

Or include ignored files too:
git stash -a

View Changes in Last Commit

Viewing
See what changed in the most recent commit:
git show HEAD

Or just the files:
git show --name-only HEAD

Or with stats:
git show --stat HEAD

Find When a Bug Was Introduced

Debugging
Use git bisect to find the problematic commit:
git bisect start
git bisect bad          # Current commit is bad
git bisect good <hash>   # This commit was good
# Git checks out middle commit
# Test and mark as good or bad
git bisect good/bad
# Repeat until found
git bisect reset

Clean Up Merged Branches

Maintenance
Delete all branches that have been merged:
git branch --merged | grep -v "\*\|main\|master" | xargs -n 1 git branch -d

Or for remote branches:
git branch -r --merged | grep -v "main\|master" | sed 's/origin\///' | xargs -n 1 git push origin --delete

Interactive Rebase to Clean History

History
Squash multiple commits into one:
git rebase -i HEAD~3

In the editor, change 'pick' to 'squash' for commits you want to combine.

Or reword commit messages:
Change 'pick' to 'reword' for commits to edit.

See What Changed Between Branches

Comparison
Compare two branches:
git diff branch1..branch2

Or see commits in one but not the other:
git log branch1..branch2

Or see files that differ:
git diff --name-only branch1..branch2

Partial File Staging

Staging
Stage only parts of a file:
git add -p <file>

Git will show each change and ask:
y - stage this hunk
n - don't stage this hunk
s - split into smaller hunks
e - manually edit the hunk

Create Empty Commit

CI/CD
Sometimes you need an empty commit (e.g., to trigger CI):
git commit --allow-empty -m "Trigger CI build"

Useful for CI/CD pipelines that trigger on commits.

Find Large Files in History

Optimization
Find files taking up space in your repository:
git rev-list --objects --all | git cat-file --batch-check='%(objecttype) %(objectname) %(objectsize) %(rest)' | awk '/^blob/ {print substr($0,6)}' | sort --numeric-sort --key=2 | tail -10

This shows the 10 largest files in your Git history.

Quick Branch from Remote

Branching
Create a local branch tracking a remote branch:
git checkout -b local-branch origin/remote-branch

Or if branch names match:
git checkout remote-branch

Git automatically sets up tracking.

Productivity Hacks

Create Your Custom Git Aliases

Speed up your workflow with custom aliases. Add these to your ~/.gitconfig:

[alias]
  # Shortcuts
  st = status
  co = checkout
  br = branch
  ci = commit
  
  # Useful combinations
  unstage = reset HEAD --
  last = log -1 HEAD
  visual = !gitk
  lg = log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --all
  
  # Workflow helpers
  ac = !git add -A && git commit
  save = !git add -A && git commit -m 'SAVEPOINT'
  undo = reset HEAD~1 --mixed
  amend = commit --amend --no-edit
  
  # Branch management
  bclean = "!f() { git branch --merged ${1-master} | grep -v " ${1-master}$" | xargs git branch -d; }; f"
  bdone = "!f() { git checkout ${1-master} && git up && git bclean ${1-master}; }; f"

Common Issues & Solutions

Accidentally Committed to Wrong Branch

Solution:

# Save the commitgit log --oneline -1 # Note the commit hash# Switch to correct branchgit checkout correct-branch# Cherry-pick the commitgit cherry-pick <commit-hash>

Then reset the wrong branch: git checkout wrong-branch && git reset --hard HEAD~1

Merge Conflict Resolution

Steps:

  1. Identify conflicted files: git status
  2. Open files and resolve conflicts (look for <<<<<<< markers)
  3. Stage resolved files: git add <file>
  4. Complete merge: git commit

To abort: git merge --abort

Undo a Pushed Commit

Safe method (creates new commit):

git revert <commit-hash>

Warning: Never use git reset on commits that others have pulled!

Recover Deleted Branch

Use reflog to find the commit:

git reflog# Find the commit hash before deletiongit checkout -b recovered-branch <commit-hash>

Quick Reference Card

Daily Commands

  • git status
  • git add .
  • git commit -m "msg"
  • git push
  • git pull

Branching

  • git branch
  • git checkout -b name
  • git merge branch
  • git branch -d name

Viewing

  • git log --oneline
  • git diff
  • git show HEAD
  • git blame file

Frequently Asked Questions

What are the most essential Git commands?

The most essential Git commands are: git clone (get a repository), git add (stage changes), git commit (save changes), git push (upload to remote), git pull (download from remote), git status (check status), git branch (manage branches), and git log (view history). These cover 90% of daily Git usage.

How do I undo a Git commit?

To undo the last commit but keep changes: git reset --soft HEAD~1. To undo and discard changes: git reset --hard HEAD~1. To undo a commit that's already pushed: git revert HEAD (creates a new commit that undoes the changes). Never use reset on commits that are already pushed to shared branches.

What is the difference between git pull and git fetch?

git fetch downloads changes from remote but doesn't merge them into your working directory. git pull is equivalent to git fetch followed by git merge. Use git fetch when you want to see what changed before merging, and git pull when you want to immediately update your branch.

How do I create a Git alias?

Create Git aliases using: git config --global alias.st status (creates "st" as alias for "status"). You can also edit ~/.gitconfig directly. Popular aliases include: git config --global alias.co checkout, git config --global alias.br branch, git config --global alias.ci commit.

What is git stash and when should I use it?

git stash temporarily saves uncommitted changes so you can switch branches or pull updates. Use git stash to save changes, git stash pop to restore them, and git stash list to see all stashes. Perfect for when you need to switch branches but aren't ready to commit.

How do I resolve merge conflicts?

When conflicts occur: 1) Open conflicted files and look for conflict markers (<<<<<<, ======, >>>>>>), 2) Edit files to resolve conflicts, 3) Stage resolved files with git add, 4) Complete merge with git commit. Use git merge --abort to cancel the merge if needed.

What is the difference between git merge and git rebase?

git merge creates a merge commit combining two branches, preserving the complete history. git rebase replays commits from one branch onto another, creating a linear history. Use merge for shared branches and rebase for cleaning up local commits before pushing.

How do I see what changed in a commit?

Use git show <commit-hash> to see changes in a specific commit, git diff to see unstaged changes, git diff --staged to see staged changes, and git log -p to see commit history with diffs. git diff HEAD shows all changes since last commit.

Explore Our Developer Tools

While you're here, check out our powerful developer tools for JSON processing, API testing, and more.