Samson
Samson

Reputation: 1888

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

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.

Upvotes: 2

Views: 116

Answers (2)

tmaj
tmaj

Reputation: 35075

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.

Upvotes: 2

VonC
VonC

Reputation: 1327164

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.

Upvotes: 3

Related Questions