1

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.

Samson
  • 310
  • 1
  • 12

2 Answers2

2

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.

tymtam
  • 20,472
  • 3
  • 58
  • 92
  • This worked (almost) perfectly for me! But 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, while git checkout only works on unstaged files. – Samson Oct 11 '19 at 05:40
  • 2
    @Samson Which is why I proposed `git restore`: it will reset the working tree *and* the staging area. – VonC Oct 11 '19 at 06:13
  • @VonC I see, will try that next time, thank you so much! – Samson Oct 14 '19 at 04:34
  • 2
    @Samson If you are still with a Git older than Git 2.23, you have chosen the right solution. With Git 2.23+, the same solution is needlessly confusing. – VonC Oct 14 '19 at 04:36
2

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.

VonC
  • 1,042,979
  • 435
  • 3,649
  • 4,283