Home > OS >  How to revert a commit but to a specific folder?
How to revert a commit but to a specific folder?

Time:12-02

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?

CodePudding user response:

You can use --no-commit.

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

CodePudding user response:

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.

  •  Tags:  
  • git
  • Related