I accidentally committed the wrong files to Git, but didn't push the commit to the server yet.
How do I undo those commits from the local repository?
I accidentally committed the wrong files to Git, but didn't push the commit to the server yet.
How do I undo those commits from the local repository?
bash
git reset --hard HEAD~1
This command will remove the most recent commit and reset the branch pointer to the previous commit (`HEAD~1`).
Note: Replace `1` with the number of commits you want to undo. If you want to undo multiple commits, increase the number accordingly.
4. After running the `git reset` command, your local branch will be updated, and the most recent commits will be removed.
Caution: Be careful while using `git reset --hard` as it discards the changes permanently and cannot be undone.
Here's an example:
Let's say you have made three commits to your repository. You want to undo the last two commits and go back to the state of the repository after the first commit.
Commit C: [HEAD]
Commit B
Commit A
Running the command `git reset --hard HEAD~2` will remove the `C` and `B` commits, and the repository will be at the state of `Commit A`.Answered, September 16th, 2023
28291
$ git commit -m "Something terribly misguided" # (0: Your Accident)
$ git reset HEAD~ # (1)
[ edit files as necessary ] # (2)
$ git add . # (3)
$ git commit -c ORIG_HEAD # (4)
git reset
is the command responsible for the undo. It will undo your last commit while leaving your working tree (the state of your files on disk) untouched. You'll need to add them again before you can commit them again.git add
anything that you want to include in your new commit.reset
copied the old head to .git/ORIG_HEAD
; commit
with -c ORIG_HEAD
will open an editor, which initially contains the log message from the old commit and allows you to edit it. If you do not need to edit the message, you could use the -C
option.Alternatively, to edit the previous commit (or just its commit message), commit --amend
will add changes within the current index to the previous commit.
To remove (not revert) a commit that has been pushed to the server, rewriting history with git push origin main --force[-with-lease]
is necessary. It's almost always a bad idea to use --force
; prefer --force-with-lease
instead, and as noted in the git manual:
You should understand the implications of rewriting history if you [rewrite history] has already been published.
You can use git reflog
to determine the SHA-1 for the commit to which you wish to revert. Once you have this value, use the sequence of commands as explained above.
HEAD~
is the same as HEAD~1
. The article What is the HEAD in git? is helpful if you want to uncommit multiple commits.
Answered, May 29th, 2009
12534
Undoing a commit is a little scary if you don't know how it works. But it's actually amazingly easy if you do understand. I'll show you the 4 different ways you can undo a commit.
Say you have this, where C is your HEAD and (F) is the state of your files.
(F)
A-B-C
↑
master
git reset --hard
You want to destroy commit C and also throw away any uncommitted changes. You do this:
git reset --hard HEAD~1
The result is:
(F)
A-B
↑
master
Now B is the HEAD. Because you used --hard
, your files are reset to their state at commit B.
git reset
Maybe commit C wasn't a disaster, but just a bit off. You want to undo the commit but keep your changes for a bit of editing before you do a better commit. Starting again from here, with C as your HEAD:
(F)
A-B-C
↑
master
Do this, leaving off the --hard
:
git reset HEAD~1
In this case the result is:
(F)
A-B-C
↑
master
In both cases, HEAD is just a pointer to the latest commit. When you do a git reset HEAD~1
, you tell Git to move the HEAD pointer back one commit. But (unless you use --hard
) you leave your files as they were. So now git status
shows the changes you had checked into C. You haven't lost a thing!
git reset --soft
For the lightest touch, you can even undo your commit but leave your files and your index:
git reset --soft HEAD~1
This not only leaves your files alone, it even leaves your index alone. When you do git status
, you'll see that the same files are in the index as before. In fact, right after this command, you could do git commit
and you'd be redoing the same commit you just had.
git reset --hard
and need to get that code backOne more thing: Suppose you destroy a commit as in the first example, but then discover you needed it after all? Tough luck, right?
Nope, there's still a way to get it back. Type this
git reflog
and you'll see a list of (partial) commit shas (that is, hashes) that you've moved around in. Find the commit you destroyed, and do this:
git checkout -b someNewBranchName shaYouDestroyed
You've now resurrected that commit. Commits don't actually get destroyed in Git for some 90 days, so you can usually go back and rescue one you didn't mean to get rid of.
Answered, July 28th, 2011
How can I remove a specific item from an array in JavaScript?
How do I delete a Git branch locally and remotely?
How can I find all files containing a specific text (string) on Linux?
How to find all files containing specific text (string) on Linux?
How do I revert a Git repository to a previous commit?
How do I create an HTML button that acts like a link?
How do I check out a remote Git branch?
How do I force "git pull" to overwrite local files?
How do I get the hash for the current commit in Git?
How do I programmatically determine if there are uncommitted changes?
'git diff' doesn't give any output
How can you git pull only the current branch?
How do I 'overwrite', rather than 'merge', a branch on another branch in Git?
How to remove/delete a large file from commit history in the Git repository?
How to remove files that are listed in the .gitignore but still on the repository?
why when I use git ls-tree it shows the whole directory instead of the files of the commit?