git rebase to remove a commit

家住魔仙堡 提交于 2019-12-11 02:44:33

问题


I have the following change history in my git repository:

---X---Y---Z---A---B---C

I began working from a base of code Z and made three changes A, B and C. Each of these changes has been uploaded as separate reviews on Gerrit and each depends on the change before it.

Following a review I want to remove change A, so that my change history is:

---X---Y---Z---B---C

What is the correct flow of git rebase -i and git commit to get this to work?

I assumed the following:

  1. Use git rebase -i HEAD~3 to show the last three commits. This shows the file contents:

    pick 1234567 Commit A message
    pick 1a2b3c4 Commit B message
    pick abcdefg Commit C message

  2. I can then delete the first line and save the file to remove the first commit.

  3. This is where I get stuck... If I try to git commit --amend I can only amend the commit of C. I thus don't know how to push my rebase change to Gerrit for review using repo upload . or git push

I am using Gerrit 2.2 so there is no rebase button on the review page.


回答1:


The following steps have now worked for me:

  1. Use git rebase -i HEAD~3 to show the last three commits. This shows the file contents:

    pick 1234567 Commit A message
    pick 1a2b3c4 Commit B message
    pick abcdefg Commit C message

  2. I can then delete the first line and save the file to remove the first commit.

  3. A git log now shows my B and C change after the base of Z, with their new SHA-1 values

  4. I can then push each change individually to their respective reviews:

    git push ssh://myserver:29418/project SHA1_of_B:ref/changes/123
    git push ssh://myserver:29418/project SHA1_of_C:ref/changes/789

Where 123 and 789 are the review numbers for change B and C respectively. My B and C changes each have new patches which are properly rebased.

Turns out my problem was just understanding that I had to git push both changes individually.




回答2:


After Step 2) you're done with rebasing. After choosen which commits to pick. It is possible that you get merge conflicts. Fix them with git mergetool and do a git rebase --continue

EDIT: Because rebasing does change the history. I don't know how gerrit will react on this. You can try to add the force flag e.g. git push -f, but remember this is dangerous because of history change!



来源:https://stackoverflow.com/questions/11954635/git-rebase-to-remove-a-commit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!