I have made some changes to a file which has been committed a few times as part of a group of files, but now want to reset/revert the changes on it back to a previous versio
git checkout Last_Stable_commit_Number -- fileName
2.Git revert file to a specific branch
git checkout branchName_Which_Has_stable_Commit fileName
Many suggestions here, most along the lines of git checkout $revision -- $file
. A couple of obscure alternatives:
git show $revision:$file > $file
And also, I use this a lot just to see a particular version temporarily:
git show $revision:$file
or
git show $revision:$file | vim -R -
(OBS: $file
needs to be prefixed with ./
if it is a relative path for git show $revision:$file
to work)
And the even more weird:
git archive $revision $file | tar -x0 > $file
You can use any reference to a git commit, including the SHA-1 if that's most convenient. The point is that the command looks like this:
git checkout [commit-ref] -- [filename]
As of git v2.23.0 there's a new git restore method which is supposed to assume part of what git checkout
was responsible for (even the accepted answer mentions that git checkout
is quite confusing). See highlights of changes on github blog.
The default behaviour of this command is to restore the state of a working tree with the content coming from the source
parameter (which in your case will be a commit hash).
So based on Greg Hewgill's answer (assuming the commit hash is c5f567
) the command would look like this:
git restore --source=c5f567 file1/to/restore file2/to/restore
Or if you want to restore to the content of one commit before c5f567:
git restore --source=c5f567~1 file1/to/restore file2/to/restore
if you commit a wrong file in your last commits follow the instruction :
I think I've found it....from http://www-cs-students.stanford.edu/~blynn/gitmagic/ch02.html
Sometimes you just want to go back and forget about every change past a certain point because they're all wrong.
Start with:
$ git log
which shows you a list of recent commits, and their SHA1 hashes.
Next, type:
$ git reset --hard SHA1_HASH
to restore the state to a given commit and erase all newer commits from the record permanently.