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.
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.
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 .
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With