Search code examples
gitgit-rebase

Remove changes to file in old, already pushed, git commit?


I have an old commit, A. When I committed A, I changed 2 files: F1 and F2. I want to change A, so it only modifies file F1. I want to commit F2 separately. I do not want to delete F1 from my git repository.

I tried doing this by

git rebase -i HEAD~3 // The exact number doesn't matter, I just change "pick" to "edit" next to A
git reset HEAD^ // unstage all changes
git add F1 // don't add F2, we want to commit it separately
git rebase --continue

However, this gives the error

F2: needs update
You must edit all merge conflicts and then
mark them as resolved using git add

I solve the problem by doing git rebase --skip

However, this removes commit A from the git history completely.

What am I doing wrong?

I looked at other posts on SO, and tried the solutions, but they didn't work.


Solution

  • I almost did what @Tim's post suggested, except I had to add in 1 command. Here's what I did:

    git rebase -i HEAD~3 // The exact number doesn't matter, I just change "pick" to "edit" next to A
    git reset HEAD^ // unstage all changes
    git add F2
    git commit -m "Add F2"
    git add F1
    git commit -m "Add F1"
    git rebase --continue
    

    The git reset HEAD^ was not mentioned in the linked post.