23

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.

  • That question (despite the title) pertains to git-checkout. A checkout restores a file to a previous version, removing all commits after that point.
  • My question pertains to git-revert. A revert undoes changes made in a particular commit, without touching other commits that may have come later. It applies the reverse of (only) that commit.
Community
  • 1
  • 1
Paul Draper
  • 78,542
  • 46
  • 206
  • 285
  • 1
    Can you explain your need to `revert` instead of using `checkout`? As far as I know, a checkout of the appropriate files followed by a commit would be equivalent to a revert. – ChrisGPT was on strike Apr 14 '14 at 19:47
  • 2
    I don't think you can revert a single file, where did you read that, is it in the docs? You can however apply patches in reverse, together with git apply: `git show -- | git apply -R`, found here: http://git.661346.n2.nabble.com/Revert-a-single-commit-in-a-single-file-td6064050.html – steabert Apr 14 '14 at 19:47
  • 1
    I think this might be a duplicate of this post: http://stackoverflow.com/questions/2733873/reverting-a-single-file-to-a-previous-version-in-git – Chris Maes Apr 14 '14 at 19:48
  • 1
    @ChrisMaes, see my explanation of the difference between `checkout` and `revert`. – Paul Draper Apr 14 '14 at 19:55
  • 1
    @PaulDraper, interesting, apparently I've misunderstood `revert` for years. So if you have commits `A` (older), `B`, and `C`, you want to reverse the changes from `A` while keeping the changes from the newer commits `B` and `C`. – ChrisGPT was on strike Apr 14 '14 at 19:57
  • @steabert, that is a reasonable option in my case. – Paul Draper Apr 14 '14 at 20:06

4 Answers4

33

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
James Ko
  • 32,215
  • 30
  • 128
  • 239
vcsjones
  • 138,677
  • 31
  • 291
  • 286
  • FWIW, it should be pretty easy to wrap this up in a shell script make it easier if you find yourself doing this a lot. – vcsjones Apr 14 '14 at 20:01
  • Alternatively, if you want to keep most files in the revert and only undo a few, you can do the git-revert, use `git reset --hard` on the files you don't want reverted, then commit. – vcsjones Apr 14 '14 at 20:11
11

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
jthill
  • 55,082
  • 5
  • 77
  • 137
9

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).

Community
  • 1
  • 1
torek
  • 448,244
  • 59
  • 642
  • 775
  • This certainly is the simplest way. – Paul Draper Apr 15 '14 at 23:50
  • This is less robust than the other answers: `error: patch failed: ...\n error: ...: patch does not apply` – nyanpasu64 Jul 06 '19 at 00:10
  • 1
    @jimbo1qaz: yes, that's why I said vcsjones' answer is probably best in general (though jthill's is just as good, and probably easier—it didn't exist at the time I wrote the above). – torek Jul 06 '19 at 00:23
0

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
chipfall
  • 300
  • 2
  • 6