开发者

git: removing changes introduced by a series of bad commits

开发者 https://www.devze.com 2023-01-28 09:00 出处:网络
say i have a git repo, with such commit history: A-B-C-D-E Now, suddenly i realise, that commits B and C are completely flawed and unnecessary.

say i have a git repo, with such commit history: A-B-C-D-E

Now, suddenly i realise, that commits B and C are completely flawed and unnecessary. Also, say i have the complete freedom to rewrite project history (perhaps i'm working on the project alone).

I have another branch pointing to the commits B and C, so losing them is not a problem. If i ever actually need those changes, i can checkout that branch and see them.

But for the master branch, i would like to force the history to be A-D'-E', where D and E not only have a modified parents list, but also do not contain the changes that were introduced by co开发者_如何学JAVAmmits B and C.

Is this possible to do? And if so, how?


git rebase -i HEAD~5

Then, remove the unwanted commits from the list of commits


I'm going to assume there are 5 commits.

In which case you'll do a:

git rebase -i HEAD~5

Then you'll remove the 2 lines pertaining to commits B and C.

Finally, you'll do a:

git rebase --continue


git rebase --onto $commit_A $commit_C $commit_E

This says “take all the commits after C, up to and including E, and replay them on top of A”.

You can use any notation for the commits you are referring to, of course. Eg. if master is at E and you have a branch pointing at C, say backup, you can also say this:

git rebase --onto $commit_A backup master

This is even more convenient as you will be left with HEAD pointed at master, and master in turn pointed at the re-written E.

0

精彩评论

暂无评论...
验证码 换一张
取 消