I accidentally committed the wrong files to Git, but I haven't pushed the commit to the server yet.
How can I undo those commits from the local repository?
I accidentally committed the wrong files to Git, but I haven't pushed the commit to the server yet.
How can I undo those commits from the local repository?
Use SourceTree (graphical tool for Git) to see your commits and tree. You can manually reset it directly by right clicking it.
There are many ways to do it:
Git command to undo the last commit/ previous commits:
Warning: Do Not use --hard if you do not know what you are doing. --hard is too dangerous, and it might delete your files.
Basic command to revert the commit in Git is:
or
COMMIT-ID: ID for the commit
n: is number of last commits you want to revert
You can get the commit id as shown below:
where d81d3f1 and be20eb8 are commit id.
Now let's see some cases:
Suppose you want to revert the last commit 'd81d3f1'. Here are two options:
or
Suppose you want to revert the commit 'be20eb8':
For more detailed information you can refer and try out some other commands too for resetting head to a specified state:
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.
Say you have this, where C is your HEAD and (F) is the state of your files.
You want to nuke commit C and never see it again. You do this:
The result is:
Now B is the HEAD. Because you used
--hard
, your files are reset to their state at commit B.Ah, but suppose 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:
You can do this, leaving off the
--hard
:In this case the result is:
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 nowgit status
shows the changes you had checked into C. You haven't lost a thing!For the lightest touch, you can even undo your commit but leave your files and your index:
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 dogit commit
and you'd be redoing the same commit you just had.One 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
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: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.
Type
git log
and find the last commit hash code and then enter:In my case I accidentally committed some files I did not want to. So I did the following and it worked:
Verify the results with gitk or git log --stat
Simple, run this in your command line: