How can I do a git add --patch during an interactive rebase?
Asked Answered
R

2

9

I want to go back and remove several portions of a commit that is two commits back. I hoped I could do git rebase -i HEAD^^, edit the commit, and then use git add --patch <file> on the file. However, during the rebase, git reset HEAD <file> doesn't appear to work because when I try git add --patch <file>, it says there are no changes.

Raillery answered 25/10, 2015 at 18:3 Comment(0)
K
1

During rebase HEAD points to the latest commit that has been added onto the base. So git reset head between two rebase operation does nothing.

You need to reset to 1 commit before with git reset HEAD^ and then (interactively) add the desired changes.

$ git rebase -i ... # change a commit to "edit"
$ git reset HEAD^
$ git add --patch
$ git commit

Possibly discard all remaining changes that were not commited:

$ git checkout .
Koloski answered 13/9, 2020 at 17:19 Comment(0)
R
7

The issue is that, during an interactive rebase HEAD does not point to the previous commit, so git reset HEAD doesn't do anything.

Instead, find the hash of the previous commit using git log and then just run git reset <hash> <file>, followed by git add --patch <file>.

You can then run git checkout -- <file> to discard the rest of the changes.

Raillery answered 25/10, 2015 at 18:3 Comment(2)
surprised that this question/answer didn't get more votes?Nightclub
HEAD never points to the previous commit but always to the latest one. Using the revision hash works but is not necessary.Nopar
K
1

During rebase HEAD points to the latest commit that has been added onto the base. So git reset head between two rebase operation does nothing.

You need to reset to 1 commit before with git reset HEAD^ and then (interactively) add the desired changes.

$ git rebase -i ... # change a commit to "edit"
$ git reset HEAD^
$ git add --patch
$ git commit

Possibly discard all remaining changes that were not commited:

$ git checkout .
Koloski answered 13/9, 2020 at 17:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.