How to deal with a file getting added to a wrong commit in git?

git

Solution

That's how I would have dealt with your problem:

Start an interactive rebase:

git rebase -i COMMIT-OF-FEAT-X^ # notice the ^ at the end

Then change pick to edit for the concerned commit. You would then be in (please suggest a better wording) that specific commit.

Remove the file from the commit:

git remove --cached wrong_file

That file would then be untracked.

Continue the rebase:

git rebase --continue

You're done.

You can now just switch to the feat-Y branch and add the file, commit it, or do whatever you want with it:

git co feat-Y
git add wrong_file

Problem

I have a past commit in my git history where I was working on a feature 'X' and committed a file accidentally which had nothing to do with this feature along with the other relevant files. Now I am working on the feature 'Y' which is actually related to the file which got committed earlier by mistake. How to deal with this scenario in the best way?

Original source