πŸ“‹ Cheat Sheets

Git Cheat Sheet β€” Every Command You Actually Need


Click any command to expand the explanation and examples.

πŸ“¦ Setup & Config

git init basics
Create a new Git repository in the current directory.
git init
git init my-project
Creates a hidden .git folder. Run this once per project.
git clone <url> basics
Download a repository from GitHub/GitLab to your machine.
git clone https://github.com/user/repo.git
git clone git@github.com:user/repo.git
git clone https://github.com/user/repo.git my-folder
git config basics
Set your name and email (required for commits).
git config --global user.name "Your Name"
git config --global user.email "you@example.com"

Check current config

git config β€”list

Set for this repo only (no β€”global)

git config user.email β€œwork@company.com”

πŸ“Έ Staging & Committing

git status daily
See what's changed, staged, and untracked.
git status
git status -s  # short format
Run this constantly. It's your "where am I?" command.
git add daily
Stage files for the next commit.
git add file.txt          # stage one file
git add src/              # stage a directory
git add -A                # stage everything (new, modified, deleted)
git add -p                # interactively stage parts of files
git add -p is underrated β€” it lets you commit only part of a file's changes.
git commit daily
Save staged changes with a message.
git commit -m "Add login page"
git commit -am "Fix typo"          # add + commit tracked files
git commit --amend                  # edit the last commit
git commit --amend --no-edit        # add staged files to last commit
--amend rewrites history β€” only use on unpushed commits.
git diff daily
See what changed.
git diff                  # unstaged changes
git diff --staged         # staged changes (about to commit)
git diff main..feature    # difference between branches
git diff HEAD~3           # changes in last 3 commits
git stash useful
Temporarily save uncommitted changes.
git stash                 # stash changes
git stash pop             # restore and remove from stash
git stash list            # see all stashes
git stash apply           # restore but keep in stash
git stash drop            # delete a stash
Use when you need to switch branches but aren't ready to commit.

🌿 Branches

git branch daily
List, create, or delete branches.
git branch                # list local branches
git branch -a             # list all (including remote)
git branch feature        # create new branch
git branch -d feature     # delete (safe β€” won't delete unmerged)
git branch -D feature     # force delete
git branch -m old new     # rename a branch
git checkout / git switch daily
Switch between branches.
# Modern way (Git 2.23+)
git switch main
git switch -c new-feature     # create and switch

Classic way

git checkout main git checkout -b new-feature # create and switch

Restore a file

git checkout β€” file.txt # discard changes to file

git switch is the modern replacement for git checkout for branch switching.

git merge daily
Combine branches.
git merge feature             # merge feature into current branch
git merge --no-ff feature     # always create a merge commit
git merge --abort             # cancel a merge with conflicts
git rebase advanced
Replay commits on top of another branch. Creates a cleaner history than merge.
git rebase main               # rebase current branch onto main
git rebase -i HEAD~3          # interactive rebase last 3 commits
git rebase --abort            # cancel if something goes wrong
⚠️ Never rebase commits that have been pushed and shared with others.

🌐 Remote (GitHub/GitLab)

git push daily
Upload commits to remote.
git push                          # push current branch
git push origin main              # push to specific remote/branch
git push -u origin feature        # push and set upstream (first time)
git push --force-with-lease       # safe force push (checks for others' changes)
Never use git push --force on shared branches. Use --force-with-lease instead.
git pull daily
Download and merge remote changes.
git pull                      # fetch + merge
git pull --rebase             # fetch + rebase (cleaner history)
git pull origin main          # pull specific branch
git pull --rebase avoids unnecessary merge commits.
git fetch useful
Download remote changes without merging.
git fetch                     # fetch all remotes
git fetch origin              # fetch specific remote
git fetch --prune             # remove deleted remote branches
Use fetch when you want to see what changed before merging.
git remote setup
Manage remote connections.
git remote -v                         # list remotes
git remote add origin <url>           # add a remote
git remote set-url origin <new-url>   # change remote URL
git remote remove origin              # remove a remote

πŸ” History & Inspection

git log daily
View commit history.
git log                           # full log
git log --oneline                 # compact (one line per commit)
git log --oneline --graph         # visual branch graph
git log --author="name"           # filter by author
git log -5                        # last 5 commits
git log -- file.txt               # history of specific file
git show useful
Show details of a specific commit.
git show abc1234              # show commit details + diff
git show HEAD                 # show latest commit
git show main:file.txt        # show file from another branch
git blame useful
See who changed each line of a file.
git blame file.txt
git blame -L 10,20 file.txt   # only lines 10-20

βͺ Undoing Things

git reset advanced
Undo commits or unstage files.
git reset HEAD file.txt       # unstage a file
git reset --soft HEAD~1       # undo last commit, keep changes staged
git reset --mixed HEAD~1      # undo last commit, keep changes unstaged
git reset --hard HEAD~1       # undo last commit, DELETE changes
⚠️ --hard permanently deletes changes. Use with caution.
git revert safe
Create a new commit that undoes a previous commit. Safe for shared branches.
git revert abc1234            # revert a specific commit
git revert HEAD               # revert the last commit
Unlike reset, this doesn't rewrite history β€” safe to use on pushed commits.
git restore daily
Discard changes to files (Git 2.23+).
git restore file.txt              # discard unstaged changes
git restore --staged file.txt     # unstage a file
git restore --source=HEAD~1 file.txt  # restore from previous commit

🏷️ Tags

git tag releases
Mark specific commits (usually for releases).
git tag v1.0.0                    # lightweight tag
git tag -a v1.0.0 -m "Release"   # annotated tag (recommended)
git tag                           # list tags
git push origin v1.0.0            # push a tag
git push origin --tags            # push all tags

Quick Reference Table

What you want to doCommand
Start a new repogit init
Download a repogit clone <url>
See what changedgit status
Stage everythinggit add -A
Commitgit commit -m "message"
Push to GitHubgit push
Pull latest changesgit pull
Create a branchgit switch -c name
Switch branchesgit switch name
Merge a branchgit merge name
Undo last commit (keep changes)git reset --soft HEAD~1
Discard all local changesgit restore .
See commit historygit log --oneline