I have made some changes to a file which has been committed a few times as part of a group of files, but now want to reset/revert the changes on it back to a previous versio
Many answers here claims to use git reset ...
or git checkout ...
but by doing so, you will loose every modifications on
committed after the commit you want to revert.
If you want to revert changes from one commit on a single file only, just as git revert
would do but only for one file (or say a subset of the commit files), I suggest to use both git diff
and git apply
like that (with
= the hash of the commit you want to revert) :
git diff ^ path/to/file.ext | git apply -R
Basically, it will first generate a patch corresponding to the changes you want to revert, and then reverse-apply the patch to drop those changes.
Of course, it shall not work if reverted lines had been modified by any commit between
and HEAD
(conflict).