Find the right Git command by what you want to do, copy it in one click, and let the fix-it wizard write the exact steps for common rescues like undoing a commit, renaming a branch or squashing commits.
Last updated:
Your commands
Advertisement
Advertisement
How to use the Git Cheat Sheet
Search or browse: type what you want to do (undo, rename, stash, tag) or pick a category tab.
Copy a command with the Copy button and replace the highlighted placeholders such as <branch> with your own values.
Use the fix-it wizard when you are in a specific situation: choose it from the list, answer the questions and fill in your names.
Follow the generated steps in order. Copy them one at a time or all at once, and read any warning before running a command marked as dangerous.
Download the cheat sheet as a Markdown file to keep it next to your project.
git reset vs git revert vs git restore
These three commands all "undo" something, which is why they are easy to mix up. The key question is whether the commit has already been shared.
Command
What it changes
Rewrites history?
Use it when
git restore <file>
Working files (or the staging area with --staged)
No
You want to throw away or unstage edits that are not committed yet
git reset --soft HEAD~1
Moves the branch back; changes stay staged
Yes
The last commit is local and you want to redo it
git reset HEAD~1 (mixed)
Moves the branch back; changes stay in your files, unstaged
Yes
You want to split or re-stage the work of a local commit
git reset --hard HEAD~1
Moves the branch back and deletes the changes
Yes
You want the commit and its work gone (recoverable only through reflog)
git revert <commit>
Adds a new commit with the opposite changes
No
The commit is already pushed to a branch other people use
How Git moves your changes
Git keeps your work in four places. Most commands move changes from one place to the next:
Working directory: the files you edit. git restore discards changes here.
Staging area (index): what will go into the next commit. git add moves changes in, git restore --staged moves them out.
Local repository: your commits. git commit records the staged snapshot; git reset moves the branch pointer back.
Remote repository: the shared copy, such as GitHub or GitLab. git push uploads commits, git fetch downloads them and git pull downloads and merges.
Reading HEAD~ and HEAD^ references
Reference
Means
HEAD
The commit you currently have checked out
HEAD~1 or HEAD~
The parent of HEAD (one commit back)
HEAD~3
Three commits back, following first parents
HEAD^2
The second parent of a merge commit (the branch that was merged in)
main..feature
Commits on feature that are not on main
origin/main
Your last-fetched copy of main on the remote called origin
@{u}
The upstream branch of the current branch
stash@{0}
The most recent stash entry
A short rule that prevents most Git disasters: rewrite history (reset, commit --amend, rebase) only on commits that exist nowhere but your computer or your own branch, and use git revert for anything others may already have pulled.
Frequently asked questions
How do I undo the last commit in Git?
If you have not pushed it, run git reset --soft HEAD~1 to remove the commit but keep its changes staged, or git reset HEAD~1 to keep them unstaged. If the commit is already pushed to a shared branch, run git revert HEAD instead, which adds a new commit that reverses it without rewriting history.
What is the difference between git reset and git revert?
git reset moves the branch pointer back, removing commits from the branch history, so it should only be used on commits nobody else has. git revert creates a new commit that applies the opposite changes, so history is preserved and it is safe on pushed, shared branches.
How do I rename a Git branch locally and on the remote?
Rename it locally with git branch -m old-name new-name, push the new name with git push origin -u new-name, then delete the old remote branch with git push origin --delete old-name. Teammates should then run git fetch --prune and switch to the new branch.
What is the difference between git fetch and git pull?
git fetch downloads new commits and branches from the remote but does not change your working files. git pull runs a fetch and then merges (or rebases, with --rebase) the remote branch into your current branch.
Is git push --force dangerous?
It overwrites the remote branch with your local one, which can delete commits other people pushed. Prefer git push --force-with-lease, which refuses to overwrite the remote if it has commits you have not fetched, and never force push to a shared main branch.
Should I use git switch or git checkout?
Both work. git switch (for branches) and git restore (for files) were added in Git 2.23 to split the many jobs of git checkout into clearer commands. git checkout still works in every version, so older tutorials and scripts use it.
Can I recover a branch or commit I deleted?
Usually yes. Run git reflog to see where HEAD has pointed recently, find the commit hash from before the deletion, then run git branch recovered-name <hash>. Reflog entries are kept for 90 days by default, so act before garbage collection removes them.
Are all the tools on FreeToolCabin tested for quality and accuracy?
Yes! Every tool on our site undergoes automated Q/A testing in real browser environments to ensure high precision, correct output, and zero errors. If you happen to spot a bug or have a suggestion, please contact us at [email protected] and our team will review it right away.