u_ser722345
u_ser722345

Reputation: 669

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: 27

Views: 43741

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: 3064

Try

git checkout HEAD~ filename

instead.

Upvotes: 6

amalloy
amalloy

Reputation: 92147

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: 141998

You need a space between -- and your filename:

git checkout -- filename

Upvotes: 6

Related Questions