问题
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:
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
I can then delete the first line and save the file to remove the first commit.
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 usingrepo upload .
orgit 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:
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
I can then delete the first line and save the file to remove the first commit.
A
git log
now shows my B and C change after the base of Z, with their new SHA-1 valuesI 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