Undo Git mistakes
Take back a commit, a change or a bad push. 14 commands.
- Undo your last commit (not pushed yet)Take back the last commit. Keep the changes to fix them up, or throw them away.
- Throw away changes you haven't committedPut files back to how they were at the last commit, and remove new files you don't want.
- Undo a commit you already pushedSafely cancel out the last commit on GitHub with a new commit, without rewriting history.
- Add a forgotten file to the last commitForgot a file in your last commit? Add it and fold it into that commit with git commit --amend.
- Change your last commit messageFix a typo or a vague message in the commit you just made.
- Delete untracked files in GitRemove new files and folders Git isn't tracking, after checking the list first.
- Fix "fatal: not a git repository"You're running Git in a folder that isn't a Git project. Move into the project, or start a new repo.
- Fix "refusing to merge unrelated histories"Happens when you pull a GitHub repo into a folder that already has its own commits, like a new repo created with a README.
- Fix "rejected: non-fast-forward" when you pushGitHub has commits you don't have yet. Pull them in first, then push.
- Get one file back from an older commitBring back an older version of a single file with git restore, without undoing anything else in your project.
- Rebase your branch onto mainReplay your branch's work on top of the newest main, so the history reads as one straight line.
- Recover a deleted branch or lost commitFind a commit you thought was gone with git reflog, then bring it back on a branch.
- Revert an older commit that is already pushedCancel out one commit from further back in the history, without rewriting what others already have.
- Squash your last few commits into oneTidy several small commits into a single one before you share your branch.