Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undo working copy modifications of one file in Git?

After the last commit, I modified a bunch of files in my working copy, but I want to undo the changes to one of those files, as in reset it to the same state as the most recent commit.

However, I only want to undo the working copy changes of just that one file alone, nothing else with it.

How do I do that?

like image 224
hasen Avatar asked Mar 28 '09 05:03

hasen


People also ask

How do I remove changes from one file in git?

Try Git checkout --<file> to discard uncommitted changes to a file. Git reset --hard is for when you want to discard all uncommitted changes. Use Git reset --hard <commit id> to point the repo to a previous commit.

How do I revert to a previous version of a file in git?

To move HEAD around in your own Git timeline, use the git checkout command. There are two ways to use the git checkout command. A common use is to restore a file from a previous commit, and you can also rewind your entire tape reel and go in an entirely different direction.

How do I undo a file change?

If you have committed changes to a file (i.e. you have run both git add and git commit ), and want to undo those changes, then you can use git reset HEAD~ to undo your commit.


1 Answers

You can use

git checkout -- file 

You can do it without the -- (as suggested by nimrodm), but if the filename looks like a branch or tag (or other revision identifier), it may get confused, so using -- is best.

You can also check out a particular version of a file:

git checkout v1.2.3 -- file         # tag v1.2.3 git checkout stable -- file         # stable branch git checkout origin/master -- file  # upstream master git checkout HEAD -- file           # the version from the most recent commit git checkout HEAD^ -- file          # the version before the most recent commit 
like image 157
Brian Campbell Avatar answered Sep 22 '22 11:09

Brian Campbell