Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git revert certain files

Tags:

git

git-revert

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.
like image 901
Paul Draper Avatar asked Apr 14 '14 19:04

Paul Draper


People also ask

Does git revert change files?

If a file has been changed and then staged via git add , then you use git reset to pull the most recently committed version of the file and undo the changes that you've made. Data tip: HEAD refers to the most recent version of your file. You can also revert to an older version using HEAD~1, HEAD~2 etc.


2 Answers

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
like image 73
vcsjones Avatar answered Oct 24 '22 05:10

vcsjones


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
like image 37
jthill Avatar answered Oct 24 '22 05:10

jthill