This git cheat sheet lists the Git commands you'll use most, grouped by task, with a short note on what each one does. Search for a word such as undo, branch or push, pick a topic, or scroll the whole list. When you want a paper copy beside your keyboard, press Print. The commands follow the official cheat sheet and documentation on git-scm.com, and they work the same in a terminal on Windows, macOS and Linux.
How to use the git cheat sheet
- Type in the search box to filter by any word in a command's name, the command itself or its note.
- Choose a topic to see one group, such as Undoing changes or Branches.
- Read the command on the right and what it does on the left. Parts in angle brackets, such as <file> or <commit>, are placeholders for your own names.
- Press Print for a clean printed copy of what's shown.
The everyday Git workflow
Most work with Git follows the same loop. You change some files, check them with git status, stage the changes you want with git add, and save a snapshot with git commit. The staging area sits between your files and your history, so you can build each commit carefully from only the changes that belong together. When you work with other people, git pull brings their commits in and git push sends yours out.
- git switch -c my-feature
- Edit files, then git status
- git add -p to stage the right changes
- git commit -m 'Describe the change'
- git push -u origin my-feature the first time, then just git push
switch and restore, or checkout and reset?
Older guides use git checkout for almost everything: changing branches, creating branches and throwing away changes to files. Newer versions of Git split those jobs between git switch, for branches, and git restore, for files. The official cheat sheet lists both forms side by side, and this page does the same in the notes, so you can recognize either one. For example, git switch -c name does the same as git checkout -b name, and git restore --staged file does the same as git reset file.
Referring to a commit
Wherever a command takes a commit, you can use a branch name such as main, a tag such as v0.1, a commit ID such as 3e887ab, a remote branch such as origin/main, HEAD for the current commit, or HEAD~3 (also written HEAD^^^) for three commits back. So git diff HEAD~3 compares your files with the version from three commits ago.
Undoing things safely
The Pro Git book warns that undoing is one of the few areas of Git where you can lose work if you get it wrong. A few rules keep you safe:
- git commit --amend replaces the last commit rather than adding a new one. Only amend commits you haven't pushed, because changing pushed history causes problems for your collaborators.
- git reset HEAD^ undoes the last commit but leaves your files as they are, so nothing is lost.
- git reset --hard and git restore throw away changes in your files. Changes that were never committed can't be brought back.
- git stash is the gentle option: it puts your changes aside so you can bring them back later.
- git reflog lists where a branch has pointed, which helps you find a commit again after a rebase goes wrong.
Merge or rebase?
Both bring work from one branch into another. git merge joins the branches and keeps both histories as they were, adding a merge commit when needed. git rebase replays your commits on top of the other branch, giving a straight line of history. A squash merge, with git merge --squash, turns a whole branch into one new commit. When two branches haven't diverged, a merge simply moves the branch forward, which is called a fast-forward.
Settings and important files
Set your name and email once with git config --global, because Git records them in every commit. Settings for a single repository live in .git/config, and your personal settings for every repository live in ~/.gitconfig. The .gitignore file lists files and folders Git should leave alone, such as build output, logs and secrets.
Tips
- Commit small and often. Small commits are easier to review, revert and cherry-pick.
- Write clear messages. Say what changed and why, in a short first line.
- Check before you commit. git diff --staged shows exactly what will go in.
- Prefer --force-with-lease over a plain force push, so you don't overwrite someone else's work.
Common mistakes
- Committing on the wrong branch. Run git status first; it names the current branch.
- Amending pushed commits. Amend only local commits.
- Using reset --hard to unstage. Use git restore --staged instead, which keeps your changes.
- Forgetting -u on the first push, so later pushes and pulls don't know which remote branch to use.
What next
Use this often? Add Git Cheat Sh to your Home screen.
Frequently asked questions
What is in this git cheat sheet?
The commands for starting a repository, staging, committing, branches, comparing changes, undoing, history, merging, remotes and settings, each with a short note.
What is the difference between git switch and git checkout?
git switch only changes or creates branches. git checkout does that and also restores files. Newer Git versions split those jobs between switch and restore.
How do I undo the last commit in Git?
Run git reset HEAD^. It removes the last commit but keeps your files as they are, so you can change and commit again.
How do I unstage a file?
Run git restore --staged followed by the file name, or git reset with the file name. Your changes stay in the file.
Can I print the git cheat sheet?
Yes. Filter it if you like, then press Print for a clean copy of the commands shown.
Was this tool helpful?
What went wrong?
We only receive the reason you pick, never what you typed into the tool.
Thanks for your feedback.