Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 752
drs Avatar asked Oct 25 '15 18:10

drs


2 Answers

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.

like image 65
drs Avatar answered Sep 25 '22 17:09

drs


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 .
like image 37
Daniel Böhmer Avatar answered Sep 21 '22 17:09

Daniel Böhmer