Quick references
Git Pro PDF Git Cheat Sheet PDF

Git is easiest when you think in snapshots, not files.

Mental model

The three places that matter:

AreaWhat it meansCommand to inspect
Working treefiles you are editing right nowgit status
Index / staging areawhat will go into the next commitgit diff --cached
Commit historysaved snapshotsgit log --oneline --graph --decorate

Useful interpretation:

  • git add means “move this change into the next snapshot”.
  • git commit means “save the staged snapshot”.
  • git push means “publish local commits to the remote”.
  • git pull --rebase means “bring remote commits in, then replay mine on top”.

Daily workflow

The loop I actually use:

git status
git add -p
git commit -m "meaningful message"
git pull --rebase
git push

Why this loop is good:

  • git status prevents confusion.
  • git add -p forces clean, reviewable commits.
  • git pull --rebase usually keeps history linear and easier to read.

Commands worth memorizing

Inspect

git status
git diff
git diff --cached
git log --oneline --graph --decorate --all
git show HEAD

Stage and commit

git add path/to/file
git add -p
git commit -m "message"
git commit --amend

Branching

git branch
git switch -c new-branch
git switch main
git merge other-branch
git rebase main

Remote work

git fetch
git pull --rebase
git push
git push -u origin new-branch

Undo safely

The important distinction is whether the change is committed and whether it has been pushed.

SituationSafer commandWhat it does
Throw away unstaged edits in one filegit restore filereset file in working tree
Unstage somethinggit restore --staged fileremove it from index
Edit the last commit message/contentgit commit --amendrewrite most recent local commit
Undo a published commitgit revert <commit>create a new inverse commit
Move branch pointer backwards locallygit reset --soft HEAD~1keep changes, remove commit

Short rule:

  • If it is already on GitHub, prefer git revert.
  • If it is still local, restore, amend, or reset --soft are usually cleaner.

Branches and remotes

What a branch actually is

A branch is just a movable pointer to a commit.
Creating a branch is cheap; use them freely.

A remote is just another repo

Usually:

  • origin = your GitHub repository
  • main or master = primary branch
  • feature branch = temporary branch for one task

Clean pattern:

git switch main
git pull --rebase
git switch -c fix/readme-links

Handy aliases and config

git config --global pull.rebase true
git config --global rebase.autoStash true
git config --global alias.lg "log --oneline --graph --decorate --all"
git config --global alias.st "status -sb"

What these do:

  • pull.rebase true: git pull rebases by default.
  • rebase.autoStash true: temporarily stashes dirty changes during rebase.
  • git lg: compact history graph.
  • git st: shorter status command.

Practical habits

  • Commit small units of thought.
  • Write commit messages that describe why, not only what.
  • Use branches for experiments.
  • Read git status before and after every major command.
  • Prefer git add -p over git add . when the change matters.
  • Avoid force push on shared branches unless you are certain.

When I forget what to do

My fallback sequence:

git status
git log --oneline --graph --decorate -10
git diff
git diff --cached

These four commands usually tell you where you are, what changed, and what Git thinks is about to happen.