Reputation: 83293
I want to do a revert of a commit, but only for some files. (Not a checkout; a revert. If you are unfamiliar with the difference, keep reading.)
I tried this
git revert --no-commit abcdef123456 -- my/path/to/revert
And I got this error
fatal: ambiguous argument 'my/path/to/revert': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions
But that is precisely what I did! (And yes, my/path/to/revert
is in my working tree.)
My working theory is that it is not possible to revert only some files, and that the Git error message is misleading.
(Git 1.7.9.5)
This is not a duplicate of Reverting a single file to a previous version in git.
Upvotes: 23
Views: 10036
Reputation: 360
Git works by commits. You cannot git revert a file. Even if there's just one file in a commit, you are still reverting the commit. The solution is to emulate git revert to get back what you need for a new commit. I don't see the cleanest and safest way shown here - revert on another branch and checkout what you need. Unlike git apply, it won't break if there are merge conflicts and revert/reset really only lends itself to undoing one commit.
git checkout -b tmpbranch
git revert commitref # complete commit, follow with more reverts as needed
git checkout mainbranch
git checkout tmpbranch -- file/i/need # while on mainbranch and to index by default
git commit -m "this is a new commit message"
git branch -d tmpbranch
Upvotes: 0
Reputation: 141648
I don't think git lets you specify particular files to revert. The best I can think of is this:
git revert --no-commit <commit hash> # Revert, don't commit it yet
git reset # Unstage everything
git add yourFilesToRevert # Add the file to revert
git commit -m "commit message"
git reset --hard # Undo changes from the part of the revert that we didn't commit
Upvotes: 35
Reputation: 60323
A shorter sequence for when you can make a short list of what you want:
git revert that_commit # do the whole revert
git reset --hard HEAD^ # in what turns out to have been a throwaway commit
git checkout HEAD@{1} -- one/folder # and just take what you want of the results
Upvotes: 11
Reputation: 488599
vcsjones' answer is probably the best way since revert uses the three-way merge machinery. However, for many cases, you could git apply -R
(reverse) the patch to the file (or files) in question, e.g.:
git show <rev> -- path/to/file | git apply -R
(or git diff
—any diff generator that allow you to limit the result to specific files, really—but git show
is the obvious go-to for non-merge commits; for merges you'd need to specify the correct parent).
Upvotes: 9