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?
This took me a while to figure out, so maybe this will help someone...
There are two ways to "undo" your last commit, depending on whether or not you have already made your commit public (pushed to your remote repository):
How to undo a local commit
Let's say I committed locally, but now want to remove that commit.
To restore everything back to the way it was prior to the last commit, we need to
reset
to the commit beforeHEAD
:Now
git log
will show that our last commit has been removed.How to undo a public commit
If you have already made your commits public, you will want to create a new commit which will "revert" the changes you made in your previous commit (current HEAD).
Your changes will now be reverted and ready for you to commit:
For more info, check out Git Basics - Undoing Things
If you are planning to undo a local commit entirely, whatever you change you did on the commit, and if you don't worry anything about that, just do the following command.
(This command will ignore your entire commit and your changes will be lost completely from your local working tree). If you want to undo your commit, but you want your changes in the staging area (before commit just like after
git add
) then do the following command.Now your committed files come into the staging area. Suppose if you want to upstage the files, because you need to edit some wrong content, then do the following command
Now committed files to come from the staged area into the unstaged area. Now files are ready to edit, so whatever you change, you want to go edit and added it and make a fresh/new commit.
More
To change the last commit
Replace the files in the index:
Then, if it's a private branch, amend the commit:
Or, if it's a shared branch, make a new commit:
(to change a previous commit, use the awesome interactive rebase)
ProTip™: Add
*.class
to a gitignore to stop this happening again.To revert a commit
Amending a commit is the ideal solution if you need to change the last commit, but a more general solution is
reset
.You can reset git to any commit with:
Where
N
is the number of commits beforeHEAD
, and@~
resets to the previous commit.So, instead of amending the commit, you could use:
Check out
git help reset
, specifically the sections on--soft
--mixed
and--hard
, for a better understanding of what this does.Reflog
If you mess up, you can always use the reflog to find dropped commits:
For a local commit
or if you do not remember exactly in which commit it is, you might use
For a pushed commit
The proper way of removing files from the repository history is using
git filter-branch
. That is,But I recomnend you use this command with care. Read more at git-filter-branch(1) Manual Page.
Another way:
Checkout the branch you want to revert, then reset your local working copy back to the commit that you want to be the latest one on the remote server (everything after it will go bye-bye). To do this, in SourceTree I right-clicked on the and selected "Reset BRANCHNAME to this commit".
Then navigate to your repository's local directory and run this command:
This will erase all commits after the current one in your local repository but only for that one branch.
If you want to permanently undo it and you have cloned some repository
The commit id can be seen by
Then you can do -