Is it possible to perform git revert on a commit, but only in a specific directory?

Viewed 63

As mentioned in the title, I want to perform git revert on a commit that made some undesirable changes but only in a certain folder/directory.

2 Answers

With Git 2.23 (August 2019) and the new command git restore, plus the : pathspec signature:

git restore -s@~ -SW -- :path/to/folder/**

Long form:

git restore --source @~ --staged --worktree -- :path/to/folder/**

Check the result with git status, then commit.


The OP adds:

I will need to do a git reset of the files I do not want to keep changes of, before doing git checkout on them.
Doing only git checkout does not work because after git revert, the modified files are in the staging directory.

Yes, that is why git checkout is confusing, and is in the process of being replaced by:

No need for revert+reset+checkout: if you want to restore files... use git restore.

What about:

  1. git revert --no-commit
  2. git checkout everything except your folder
  3. git commit

Edit: For git >=2.23 please see VonC's answer.

Related