Taylor Fang
Taylor Fang

Reputation: 221

git checkout doesn't remove the newly added files

I want to revert my repo to exact state of a previous commit. Then make a new commit (as part of the reversion) and push to remote. I don't want to use git push -f so I don't want to rewrite the commits. I want to use one new commit and undo multiple changes.

so I ran git checkout [HASH] -- .

But I realized that this command doesn't exactly do the reversion. A file added in the later commits doesn't actually get removed.

How exactly do I revert in git?

Upvotes: 0

Views: 370

Answers (2)

LeGEC
LeGEC

Reputation: 51780

If your local git is recent enough (2.27+), git restore is the command to use :

git restore [HASH]

If for some reason, you cannot upgrade your local git to a recent enough version,
use git read-tree :

git read-tree [HASH]

Upvotes: 1

SwissCodeMen
SwissCodeMen

Reputation: 4875

It works in the same as git reset --hard, but with the important difference, that git checkout is working-directory safe, so it doesn't overwrite existing changes in your working directory.

If you wish to undo/revert a commit you can do the following, using the commit hash from the commit where you will revert:

git revert <commit hash>

But you can also clear your working directory and then you can do git checkout <commit hash>

Upvotes: 0

Related Questions