Reputation: 4113
I committed my project . But t want remove that commit but keep that code to next commit. I used this code:
// now commit is A
git reset --soft 9c74193
// 9c74193 is old commit
git commit -m "My commit" `
git push -f origin master
I want restore commit A. Can I do it ? And how ?
Thank you so much.
Upvotes: 1
Views: 40
Reputation: 45679
If this was done recently, and you still have the local repo in which you ran the above commands, then the reflog probably still has a reference to commit A
.
$ git reflog
dd3f691 HEAD@{0}: commit: My Commit
9c74193 HEAD@{1}: reset: moving to 9c74193
aaaaaaa HEAD@{2}: commit: A
$ git checkout HEAD@{2}
If you've been moving the HEAD
pointer around a lot, you could also check a branch-specific reflog
$ git reflog master
dd3f691 master@{0}: commit: My Commit
9c74193 master@{1}: reset: moving to 9c74193
aaaaaaa master@{2}: commit: A
$ git checkout master@{2}
Once you checkout A
you'll be in detached head state. You can force-move an existing branch to here if you're sure that's what you want. (For example, if the earlier force-push created problems for other developers and you're trying to fix it the way it was before, you could git branch -f master
.) Or you could create a new branch or tag here, or whatever.
Of course this may require another force push (e.g. if you are indeed moving master
back from whence it came).
Upvotes: 1