Search code examples
gitgithubgit-resetgit-revertgit-apply

Remove changes from a commit hash in a given file git


I have a 3 month old commit, lets say commit A which touchs 3 files Over the last 3 months, a lot more commits have gone in, some of which have added new code below changes from commit A

I want to remove (not revert) the changes from commit A in 1 of the 3 files.

remove as in blank line instead of code

I tried

git show < commit hash of A > file_path | git apply -R -3

this removes extra code (which is outside the scope of commit A: details below if interested)

One way i can think of is using git blame on file, wherever hash matches, remove the lines but seems to be very iterative process and time consuming.

Any pointers would be helpful..

Thanks in advance

Extra code removed from file which is out of scope of commit A:

  1. Commit A was added at the end of file
  2. New commits added more code after commit A (below changes from commit A) at the end of file
  3. now when we reverse the changes in commit A (using git apply -R -3), it cleans from start of commit A till end of file because to git, the code was originally added at the end of file

Solution

  • I would still try git-revert to remove lines in commit A.

    Next you want to restore the other two files which don’t need revert. git checkout HEAD^ - file1 file2 is the way to recover to the original file content.

    Finally commit amend to overwrite nicer message than “Revert …”