I did a commit and immediately after I noticed that I committed changes that I don't want to push to the remote .
Is there a way to undo the same by keeping those changes in my working copy to recommit them correctly?
I did a commit and immediately after I noticed that I committed changes that I don't want to push to the remote .
Is there a way to undo the same by keeping those changes in my working copy to recommit them correctly?
If you want to keep the changes:
If you also don't want to load the commit (just move the head to the previous one):
And if you don't want to keep them (and revert to the state of the previous commit, effectively destroying the last commit completely as if it never existed):
The command is
git reset <commit>
To specifically undo the last commit you can use
HEAD~1
For example to go back to the previous commit you can use the sgte command:
The parameter
--mixed
allows changes to be kept in the working tree so that they can be modified later, however, since it is the default mode, it is not necessary.In this case it seems to me the most appropriate option since you want to correct the commit so you need to revert the index to correct the changes.
Some of the optional parameters are:
--soft
it does not modify either the working tree or the index. Just change the HEAD to the indicated commit.--mixed
reverts the index but not the working tree so that changes are kept ready to be modified and possibly committed again. This is the default option.--hard
Reverts the index and the working tree so that the changes are totally lost.You can review the full syntax in the git-reset Documentation
You just do a soft reset, which keeps the changes local but undo the commit, then do whatever you want and commit again as you wanted the first time.
This is the correct way to do it:
very important, as another user mentions, this option applies when you need to keep the changes.