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 checkout
below is the answer. In git, "revert" is something you do to a commit. "Revert" replays the inverse of a historical commit into your working directory, so you can make a new commit that "undoes" the reverted commit. I find this is a frequent point of confusion for people coming to git from svn.
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.
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
git: 'restore' is not a git command.
git version 2.8.2.windows.1
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.
man bash
page. Also mentioned in this answer: unix.stackexchange.com/a/187548/142855
--
is used to tell the program I've finished specifying "options", and from here on, everything will be a positional argument.
. Conventionally, "options" are the tokens like --recursive
which can appear in any order, or even be combined together in their short form, like with rm -rf
. On the contrary, "positional arguments" are much more like arguments passed to a function in a programming language: their position in the list of tokens defines what exactly the program is going to do with them (these are often filenames). --
removes ambiguity as to which is which.
To revert to upstream/master do:
git checkout upstream/master -- myfile.txt
Reference to HEAD is not necessary.
git checkout -- file.js
is sufficient
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
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
You can use the following command:
git reset -- my-file.txt
which will update both the working copy of my-file.txt
when added.
A simple, easy, hands-on, way to get you out of hot water, especially if you're not so comfortable with git:
View the log of your file git log myFile.js commit 1023057173029091u23f01w276931f7f42595f84f Author: kmiklas Date: Tue Aug 7 09:29:34 2018 -0400 JIRA-12345 - Refactor with new architecture. Note hash of file: 1023057173029091u23f01w276931f7f42595f84f Show the file using the hash. Make sure it's what you want: git show 1023057173029091u23f01w276931f7f42595f84f:./myFile.js Redirect file to a local copy git show 1023057173029091u23f01w276931f7f42595f84f:./myFile.js > myFile.07aug2018.js Back up your current file. cp myFile.js myFile.bak.js Open both files in your favorite text editor. vim myFile.js vim myFile.07aug2018.js Copy n' paste code from myFile.07aug2018.js to myFile.js, and save. Commit and push myFile.js Again view the log, and confirm that your file is properly in place. 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.
certain circumstances
is this ideal?
Success story sharing
HEAD~1
to indicate the penultimate commit.HEAD
if you are at the head of the current branch - see norbauer.com/rails-consulting/notes/…reset
command (as it says) "cannot do hard reset with paths", and then why thecheckout
command is not (cannot be?) used for hard-resetting the whole set? (I mean why it has been designed so.)git checkout
: "Overwrite paths in the working tree by replacing with the contents in the index or in the <tree-ish>". I.e. if<tree-ish>
is omitted, whatever content in the index will be used to update the working tree. This may or may not differ from HEAD.