2

I want revert one file changes between two commit,

commit 1 hash: abcde1....
   some code changes

commit 2 hash : abcde2....
   some code changes

commit 3 hash : abcde3....
   some code changes

.....

i can use git checkout abcde3 -- file/to/restore and revert to commit 3, but lost commit 1 changes,
How can i checkout only commit 2 changes?

1 Answers1

3

You can simply use git revert.

Revert commit 2, and that will create a new commit which cancels any changes introduced by said commit 2.

If git revert (which operates at the commit level, and not for a single file) reverts too many file, you can reset the files you did not want reverted.

git revert --no-commit <commit hash> # Revert, don't commit it yet
git reset # Unstage everything
git add yourFilesToRevert # Add the file to revert
git commit -m "commit message"
git reset --hard # Undo changes from the part of the revert that we didn't commit
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • @rujgii Exactly: that is what the second part of my answer addresses: the ability to revert a specific commit (commit2) just for *one* file. – VonC Apr 15 '20 at 05:37