u_ser722345
u_ser722345

Reputation: 659

How do I revert one file to its original state in git?

I have a few files I made changes to.

Then I committed it and realized I made a change to a file I didn't want to change. How can I get the file back to its original state? I am on a branch.

git checkout --file

didn't do anything at all.

Upvotes: 26

Views: 43227

Answers (5)

reda falhi
reda falhi

Reputation: 11

You can try this :

git checkout origin/<your-main-branch> -- filename

Upvotes: -1

narcissus789
narcissus789

Reputation: 196

It isnt very clear to me what you mean by the original state. In my case however, the original state that I wanted to go back to was that of the master branch (off which I had branched off into a dev branch).

So to 'revert' a file.txt to the master branch version while checked out on a different dev branch, run:

git checkout master <full-path-to-file>\file.txt

Upvotes: 4

dyng
dyng

Reputation: 3054

Try

git checkout HEAD~ filename

instead.

Upvotes: 6

amalloy
amalloy

Reputation: 91837

git checkout filename

will work in most cases, unless you have a branch with the same name as a file. In either case,

git checkout -- filename

will be sufficient.

Upvotes: 44

johnsyweb
johnsyweb

Reputation: 141770

You need a space between -- and your filename:

git checkout -- filename

Upvotes: 6

Related Questions