Remove unwanted changes from a patch

In case you created a patch, pushed to Gerrit for review, only to discover your patch contains changes you don't wanted to be in this patch (e.g. deleted a file, staged changes to completely unrelated files), don't worry because Git always offers to another chance.

Basically you want to create a new patchset, but instead of performing your modifciations and amending them (Steps 5 to 7), you can simply revise your choice which changes to stage (and to commit, and to push). This guide assume you already fetched and checked-out your commit:

  1. Reset on the parent commit using "mixed" mode.

    Open the Git History to display the current commit (then one you want to change) and its ancestors. Right-click on the parent commit (the one right below) and select "Reset" > "Mixed (HEAD and Index)".

  2. Review the changes your commit actually made.

    Open the Git Staging view. This will list all your effective changes from the commit you want to change as "unstaged". This means that Git is aware that the files have been changed, but the decision which of actually comprise your commit has been undone.

  3. Select the changes you actually wanted.

    Simply drag the changes to actually want to be in the commit to the "Staged Changes".

  4. Clean up or reuse the rest.

    In case you want to forget about the remaining changes for good, you can right-click on them and select "Replace with HEAD revision". The HEAD revision is the current version, which we just reset to the parent of your commit (Step 1), which means: "Git, please restore this file to the state it had before I touched it with my commit."
    In case you want ot reuse the remaining changes (e.g. because they should be part of a future commit), just do nothing. Unstaged changes won't be committed (and therefore won't be pushed).

  5. Create your commit.

    You now need to create a new commit that requires a new commit message, which can be created in multiple ways (copy-paste the original commit message, writing a new one, ...). But please remember to take care of two things:

    1. Don't tick the "Amend" option, because that would amend the current HEAD, which is the parent of your original commit, and you don't want to touch this one.
    2. Reuse the same Change-Id as in your original commit. Otherwise Gerrit won't recognize your new commit as a new patch set.

You may continue as usual when creating a new patch set (starting with Step 8).