what happened: change1, git commit, git push, change2, git commit
开发者_JS百科what should have happened: change1, git commit, git push, change2, git commit --amend
It's not important that I retain change2, but it is important that I am able to amend the original commit. How do I do this?
Since you have pushed it, be aware that this is tricky and you are rewriting history.
You can git rebase -i
to before the first commit and choose edit
for the commit in the text that is presented to you. This way, you will be brought to the first commit state where you change / amend and then git will apply the next commit over it.
Or you can git reset --soft first
to go to first commit and amend it along with change2.
Or git reset --hard first
to go to the first commit and amend it without the change2
[I assume you know that Linus thinks this is evil]
$ git reset --hard HEAD^ # remove last commit
$ git commit --amend
$ git push --force <remote> <branch>
If you want the last commit to be applied afterward, then do
$ SHA1=`git rev-parse master`
$ git branch temp
# now the three commands above
$ git cherry-pick $SHA1
$ git branch -d temp
精彩评论