Hard reset of a single file

Git

Git Problem Overview


I currently have three modified files in my working directory. However I want one of them to be reset to the HEAD status.

In SVN, I'd use svn revert <filename> (followed by svn update <filename> if needed) but in Git I should use git reset --hard. However this command cannot operate on a single file.

Is there any way in Git to discard the changes to a single file and overwrite it with a fresh HEAD copy?

Git Solutions


Solution 1 - Git

You can use the following command:

git checkout HEAD -- my-file.txt

... which will update both the working copy of my-file.txt and its state in the index with that from HEAD.

-- basically means: treat every argument after this point as a file name. More details in this answer. Thanks to VonC for pointing this out.

Solution 2 - Git

Since Git 2.23 (August 2019) you can use restore (more info):

git restore pathTo/MyFile

The above will restore MyFile on HEAD (the last commit) on the current branch.

If you want to get the changes from other commit you can go backwards on the commit history. The below command will get MyFile two commits previous to the last one. You need now the -s (--source) option since now you use master~2 and not master (the default) as you restore source:

git restore -s master~2 pathTo/MyFile

You can also get the file from other branch!

git restore -s my-feature-branch pathTo/MyFile

Solution 3 - Git

Reset to head:

To hard reset a single file to HEAD:

git checkout @ -- myfile.ext

Note that @ is short for HEAD. An older version of git may not support the short form.

Reset to index:

To hard reset a single file to the index, assuming the index is non-empty, otherwise to HEAD:

git checkout -- myfile.ext

The point is that to be safe, you don't want to leave out @ or HEAD from the command unless you specifically mean to reset to the index only.

Solution 4 - Git

To revert to upstream/master do:

git checkout upstream/master -- myfile.txt

Solution 5 - Git

Reference to HEAD is not necessary.

git checkout -- file.js is sufficient

Solution 6 - Git

You can use the following command:

git checkout filename

If you have a branch with the same file name you have to use this command:

git checkout -- filename

Solution 7 - Git

you can use the below command for reset of single file

git checkout HEAD -- path_to_file/file_name

List all changed files to get path_to_file/filename with below command

git status

Solution 8 - Git

You can use the following command:

git reset -- my-file.txt

which will update both the working copy of my-file.txt when added.

Solution 9 - Git

A simple, easy, hands-on, way to get you out of hot water, especially if you're not so comfortable with git:

  1. View the log of your file

    git log myFile.js

    commit 1023057173029091u23f01w276931f7f42595f84f Author: kmiklas <[email protected]> Date: Tue Aug 7 09:29:34 2018 -0400

    JIRA-12345 - Refactor with new architecture.

  2. Note hash of file:

    1023057173029091u23f01w276931f7f42595f84f

  3. Show the file using the hash. Make sure it's what you want:

    git show 1023057173029091u23f01w276931f7f42595f84f:./myFile.js

  4. Redirect file to a local copy

    git show 1023057173029091u23f01w276931f7f42595f84f:./myFile.js > myFile.07aug2018.js

  5. Back up your current file.

    cp myFile.js myFile.bak.js

  6. Open both files in your favorite text editor.

    vim myFile.js
    vim myFile.07aug2018.js

  7. Copy n' paste code from myFile.07aug2018.js to myFile.js, and save.

  8. Commit and push myFile.js

  9. Again view the log, and confirm that your file is properly in place.

  10. Tell your clients to pull the latest, happily watch it work with the old version in place.

Not the sexiest, or most git-centric solution, and definitely a "manual" reset/reversion, but it works. It requires minimal knowledge of git, and doesn't disturb the commit history.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionEmilianoView Question on Stackoverflow
Solution 1 - GitMark LongairView Answer on Stackoverflow
Solution 2 - GitAxeEffectView Answer on Stackoverflow
Solution 3 - GitAsclepiusView Answer on Stackoverflow
Solution 4 - GitAnn RaihoView Answer on Stackoverflow
Solution 5 - GitEmanuel FriedrichView Answer on Stackoverflow
Solution 6 - GitMilad RahimiView Answer on Stackoverflow
Solution 7 - GitHariKishore KView Answer on Stackoverflow
Solution 8 - GitADDYQUView Answer on Stackoverflow
Solution 9 - GitkmiklasView Answer on Stackoverflow