CS concepts

Git Commands Cheat Sheet Explained: 7 You'll Actually Use

Updated Dec 4, 2026 · 3 min read

Most daily Git work needs seven commands: git status, git add -p, git commit --amend, git switch -c, git stash, git log --oneline --graph, and the undo pair git restore and git reflog. Restore throws away uncommitted edits for good, while reflog finds commits you thought you lost, for 90 days by default.

Git Commands Cheat Sheet reel cover

Key points

  • ●Run git status before anything else: branch, staged and changed files in one view.
  • ●git add -p stages hunk by hunk, so debug prints stay out of your commit.
  • ●Only amend commits you haven't pushed, because --amend rewrites the commit.
  • ●git restore <file> discards uncommitted edits with no copy kept; git restore --staged only unstages.
  • ●git reflog lists every place HEAD has been, so a commit lost to a bad reset can be found and restored.
Step-by-step visual sheet: Git Commands Cheat Sheet
The full visual sheet, made with Edithly.Open the interactive sheet

Seven commands, two of them for undo

You don't need fifty Git commands. Most days you need seven, and the last two are the ones that save you when something goes wrong. The visual sheet above has all of them with exact syntax, plus a quick-fix table you can scan in five seconds.

The everyday five

1. git status. It tells you which branch you're on, what's staged and what changed. Run it before you add, commit or switch. git status -sb gives the short version, one line per file.

2. git add -p. Instead of staging whole files, Git shows each change (a hunk) and asks. y stages it, n skips it, s splits it smaller, q quits. This is how you keep a stray print out of a clean commit.

3. git commit --amend. Typo in the message, or forgot a file? git add the file, then amend. git commit --amend --no-edit keeps the old message. Amending rewrites the commit, so only do it before you push.

4. git switch -c fix-login. Creates a branch and moves you onto it in one step. It's the same as the older git checkout -b fix-login. git switch - jumps back to the branch you were on before.

5. git stash and git stash pop. You're halfway through something and a hotfix comes in. git stash parks your work and gives you a clean tree. Switch, fix, come back, and git stash pop brings it back. Add -u to stash untracked files too, and git stash list shows what's parked.

Reading history

6. git log --oneline --graph. One commit per line, with the branch and merge lines drawn on the left. Add --all to see every branch, not just the current one. It's the fastest way to understand what a teammate's branch actually did.

The undo pair

7. git restore and git reflog. These two do opposite jobs, and mixing them up is how people lose work.

Situation Command
Staged the wrong file git restore --staged <file>
Want to throw away edits to a file git restore <file>
Typo in the last commit message git commit --amend
Need to switch branches mid-edit git stash, then git stash pop
Lost a commit after a reset git reflog

git restore <file> throws away uncommitted edits, and Git kept no copy of them, so there's no undo for the undo. git restore --staged <file> is the gentle one: it unstages the file and leaves your edits where they are.

git reflog is the safety net. It records every place HEAD has been, including commits that no branch points to anymore after a bad reset --hard or a deleted branch. Entries stay for 90 days by default.

git reflog
# a1b2c3d HEAD@{0}: reset: moving to HEAD~3
# e4f5g6h HEAD@{1}: commit: add login form
git reset --hard HEAD@{1}      # move back to before the reset
# or keep both and branch off it:
git switch -c rescue e4f5g6h

The trap

Reflog only knows about commits. If you never committed your edits and then ran git restore, there is nothing to find. When in doubt, commit or stash first; both are cheap, and both show up in history you can get back to.

Quick recap

Status to look, add -p to stage carefully, --amend to fix the last commit, switch -c to branch, stash to park work, log --oneline --graph to read history, and restore plus reflog to undo. Keep the visual sheet above next to your terminal until these are muscle memory.

Frequently asked questions

What does git add -p do?

It walks through your changes one hunk at a time and asks what to do with each. Type y to stage it, n to skip it, s to split it into smaller hunks and q to quit. It's the easiest way to keep unrelated edits out of a commit.

Is git switch -c the same as git checkout -b?

Yes for creating a branch. Both create a new branch and move you onto it. git switch arrived in Git 2.23 in 2019 so branch switching would have its own command, separate from restoring files.

How do I recover a commit after git reset --hard?

Run git reflog, find the line from before the reset, then run git reset --hard HEAD@{1} (or the matching entry), or create a branch at it with git switch -c rescue <sha>. Reflog entries are kept for 90 days by default.

What is the difference between git restore and git restore --staged?

git restore <file> throws away your uncommitted edits to that file, and Git has no copy to bring back. git restore --staged <file> only takes the file out of the staging area and leaves your edits alone.

Turn any problem or document into a visual sheet like this one

Paste a LeetCode problem, a PDF or a link into Edithly and get a step-by-step sheet you can study, share or quiz yourself on.

Make this sheet in Edithly, free