Skip to main content

Git Cheat Sheet

Setup

git init
Initialize a new Git repository
git clone <url>
Clone a repository
git config --global user.name "<name>"
Set global username
git config --global user.email "<email>"
Set global email

Basic Commands

git status
Show working directory status
git add <file>
Add file to staging
git add .
Add all files to staging
git commit -m "<message>"
Commit staged changes
git push origin <branch>
Push to remote
git pull origin <branch>
Pull from remote

Branching

git branch
List all branches
git branch <name>
Create new branch
git checkout <branch>
Switch to branch
git checkout -b <name>
Create and switch to branch
git merge <branch>
Merge branch into current
git branch -d <name>
Delete branch

History

git log
Show commit history
git log --oneline
Show compact history
git diff
Show unstaged changes
git diff --staged
Show staged changes
git show <commit>
Show commit details

Undo Changes

git reset <file>
Unstage file
git reset --hard
Reset to last commit
git revert <commit>
Revert a commit
git stash
Stash changes
git stash pop
Apply stashed changes

Remote

git remote -v
List remotes
git remote add origin <url>
Add remote
git fetch origin
Fetch from remote

About the Git Memo

Git has hundreds of commands and options, but daily work uses about 30 of them. This cheat sheet covers the essential Git commands every developer needs: branching, merging, staging, stashing, and fixing mistakes.

Each command includes a brief explanation and the most useful flags. Use it as a quick reference when you forget the exact syntax for rebasing, cherry-picking, or undoing a commit.

How to use

  1. Browse by category: setup, staging, branching, remotes, or history.
  2. Find the command you need.
  3. Copy it with one click.
  4. Check the flags section for advanced options.

Frequently Asked Questions

Is this a complete Git reference? ▾

It covers the 30 most-used commands for daily work: branching, merging, staging, undoing commits, and working with remotes. For advanced topics, consult the official Git documentation.

How do I undo my last commit? ▾

Use git reset --soft HEAD~1 to undo the commit but keep changes staged. Use git reset --hard HEAD~1 to undo and discard changes. Use git revert HEAD to create a new commit that undoes the previous one.