I want to revert changes made by a particular commit to a given file only.
Can I use git revert command for that?
Any other simple way to do it?
I want to revert changes made by a particular commit to a given file only.
Can I use git revert command for that?
Any other simple way to do it?
git revert
is for all file contents within a commits.
For a single file, you can script it:
#!/bin/bash
function output_help {
echo "usage: git-revert-single-file <sha1> <file>"
}
sha1=$1
file=$2
if [[ $sha1 ]]; then
git diff $sha1..$sha1^ -- $file | patch -p1
else
output_help
fi
(From the git-shell-scripts utilities from smtlaissezfaire)
Note:
another way is described here if you have yet to commit your current modification.
git checkout -- filename
git checkout
has some options for a file, modifying the file from HEAD, overwriting your change.
I would simply use the --no-commit
option to git-revert
and then remove the files you don't want reverted from the index before finally committing it. Here's an example showing how to easily revert just the changes to foo.c in the second most recent commit:
$ git revert --no-commit HEAD~1
$ git reset HEAD
$ git add foo.c
$ git commit -m "Reverting recent change to foo.c"
$ git reset --hard HEAD
The first git-reset
"unstages" all files, so that we can then add back just the one file we want reverted. The final git-reset --hard
gets rid of the remaining file reverts that we don't want to keep.