Version control Git command cheat sheet banner

Git Command Cheat Sheet

Quick Git commands for everyday version control: checking changes, staging files, committing, branching, merging, pulling, pushing, stashing and safely undoing mistakes.

Setup

Set name

git config --global user.name "Your Name"

Set email

git config --global user.email "you@example.com"

Set default branch

git config --global init.defaultBranch main

Show config

git config --list

Daily workflow

TaskCommand
Check current stategit status
Show unstaged changesgit diff
Stage one filegit add file.txt
Stage all changesgit add .
Commit staged changesgit commit -m "Message"
Show short historygit log --oneline --decorate --graph

Branches

List branches

git branch

Create branch

git branch feature-name

Switch branch

git switch feature-name

Create and switch

git switch -c feature-name

Merge branch

git merge feature-name

Delete branch

git branch -d feature-name

Remotes

TaskCommand
Clone repositorygit clone https://example.com/project.git
Show remotesgit remote -v
Add remotegit remote add origin URL
Fetch updatesgit fetch
Pull safely if fast-forward onlygit pull --ff-only
Push branchgit push origin branch-name
Push and set upstreamgit push -u origin branch-name

Inspect changes and history

Compact log

git log --oneline

Graph log

git log --oneline --graph --decorate --all

Show commit details

git show COMMIT_HASH

Compare branches

git diff main..feature-name

Stash unfinished work

Save work

git stash push -m "WIP message"

List stashes

git stash list

Apply and remove stash

git stash pop

Apply without removing

git stash apply stash@{0}

Undo safely

TaskCommandNotes
Unstage filegit restore --staged file.txtKeeps file changes.
Discard file changesgit restore file.txtReverts file to last commit.
Undo last commit, keep staged changesgit reset --soft HEAD~1Good for fixing commit messages or adding missed files.
Undo commit with new reverse commitgit revert COMMIT_HASHSafer for shared branches.
Danger: discard all local changesgit reset --hardDestructive. Use only if you really mean it.
For shared branches, prefer git revert over rewriting history with reset. Future you will thank present you.

Examples with output

Status output

$ git status
On branch main
Changes not staged for commit:
  modified:   index.html

Untracked files:
  notes.txt

Commit output

$ git commit -m "Fix header spacing"
[main a91c4d2] Fix header spacing
 1 file changed, 6 insertions(+), 2 deletions(-)

Short history

$ git log --oneline -3
a91c4d2 Fix header spacing
4b91d0e Add Git cheat sheet
c73a220 Fix quiz navigation

Branch creation

$ git switch -c update-footer
Switched to a new branch 'update-footer'

Stash output

$ git stash push -m "WIP footer"
Saved working directory and index state On main: WIP footer

$ git stash list
stash@{0}: On main: WIP footer

Unstage output

$ git restore --staged config.php
Unstaged changes after reset:
M       config.php
Safe Git workflows

Git workflows for everyday use

Check before changing

git status
git diff

Commit changes

git add file.txt
git commit -m "Update file"

Undo uncommitted file changes

git restore file.txt

Undo a commit safely

git revert COMMIT_HASH
FAQ

Frequently Asked Questions

How do I see changed files?

Use git status for a summary and git diff for detailed changes.

How do I unstage a file?

Use git restore --staged file.txt.

How do I undo a commit safely?

Use git revert COMMIT_HASH.

Should I use git reset on shared branches?

Avoid rewriting shared history unless you know exactly what you are doing.