How can I reset or revert a file to a specific revision?

前端 未结 30 1976
不思量自难忘°
不思量自难忘° 2020-11-21 11:23

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

相关标签:
30条回答
  • 2020-11-21 11:50
    1. Git revert file to a specific commit
    git checkout Last_Stable_commit_Number -- fileName
    

    2.Git revert file to a specific branch

    git checkout branchName_Which_Has_stable_Commit fileName
    
    0 讨论(0)
  • 2020-11-21 11:52

    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
    
    0 讨论(0)
  • 2020-11-21 11:54

    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]

    0 讨论(0)
  • 2020-11-21 11:54

    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
    
    0 讨论(0)
  • 2020-11-21 11:55

    if you commit a wrong file in your last commits follow the instruction :

    1. open source tree, change to this commit

    1. change the lines and find your commit that the wrong file sent as commit

    1. you can see the list of your changes in that commit
    2. select it and then click on ... buttons right-hand side ... click reverse file
    3. then you can see it on file status tab at the bottom left-hand side then click unstage:

    1. open your visual studio code and revert back by committing your removed files
    2. after them all, you can see results in your last commit in the source tree

    0 讨论(0)
  • 2020-11-21 11:56

    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.

    0 讨论(0)
提交回复
热议问题