user13317685
user13317685

Reputation:

How can i undo changes between two commits?

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?

Upvotes: 2

Views: 369

Answers (1)

VonC
VonC

Reputation: 1324258

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

Upvotes: 3

Related Questions