Revert changes to a file in a commit
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?
The cleanest way I've seen of doing this is described here
git show some_commit_sha1 -- some_file.c | git apply -R
Similar to VonC's response but using git show
and git apply
.
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.
Dropped.on.Caprica mentions in the comments:
You can add an alias to git so you can do git revert-file <hash> <file-loc>
and have that specific file be reverted.
See this gist.
[alias]
revert-file = !sh /home/some-user/git-file-revert.sh
Assuming it is ok to change the commit history, here's a workflow to revert changes in a single file in an earlier commit:
For example, you want to revert changes in 1 file ( badfile.txt
) in commit aaa222
:
aaa333 Good commit
aaa222 Problem commit containing badfile.txt
aaa111 Base commit
Rebase on the base commit, amend the problem commit, & continue.
1) Start interactive rebase:
git rebase -i aaa111
2) Mark the problem commit for edit in the editor by changing pick
to e
(for edit):
e aaa222
pick aaa333
3) Revert changes to the bad file:
git show -- badfile.txt | git apply -R
4) Add the changes & amend the commit:
git add badfile.txt
git commit --amend
5) Finish the rebase:
git rebase --continue
链接地址: http://www.djcxy.com/p/8986.html
下一篇: 在提交中将更改还原为文件