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.
Asked
Active
Viewed 1,421 times
6

drs
- 5,679
- 4
- 42
- 67
2 Answers
5
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.

drs
- 5,679
- 4
- 42
- 67
-
surprised that this question/answer didn't get more votes? – abbood Jun 13 '18 at 12:37
-
HEAD never points to the previous commit but always to the latest one. Using the revision hash works but is not necessary. – Daniel Böhmer Sep 13 '20 at 17:13
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 .

Daniel Böhmer
- 14,463
- 5
- 36
- 46