Git revert certain files

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

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

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

Tags:

Git

Git Revert