How to revert a commit but to a specific folder?

I've made commits such that

commit hash - hash1 -> changes = a/1.txt, a/2.txt, b/1.txt

Now I'm reverting this commit by git revert -m 1 hash1 but I want to remove only changes done to folder a/ but want to retain the changes inside b/

Is it possible?

Upvotes: 1

Views: 1169

Answers (2)

René Link
René Link

Reputation: 51433

You can use --no-commit.

git revert --no-commit <commit_id>
git restore --staged b/
git checkout .
git commit

Upvotes: 1

Davis Herring
Davis Herring

Reputation: 40013

git revert -m 1 hash1
# If needed, resolve conflicts and commit;
# those outside of a/ may be resolved arbitrarily.
git reset --hard HEAD^
git checkout hash1 a/
git commit -C HEAD@{1}

You could also use revert -n and then reset all other directories before committing; the recipe above is preferable only if there are many such other directories since it names only a.

Upvotes: 1

Related Questions