git rebase to remove a commit

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.

  • Where should I locate my Git repositories for projects that share common libraries?
  • How to copy SVN repository as a folder to another repository with history
  • How to remove a file from git branch
  • gitosis: same user multiple machines
  • Changing username in SVN+SSH URI on the fly in working copy
  • Pull all unfetched changes from git repository
  • Subversion repository layout for libraries developed across different programs
  • How to use repo as repository on Hudson
  • 2 Solutions collect form web for “git rebase to remove a commit”

    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.

    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!

    Git Baby is a git and github fan, let's start git clone.