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

前端 未结 30 1975
不思量自难忘°
不思量自难忘° 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:46

    Assuming the hash of the commit you want is c5f567:

    git checkout c5f567 -- file1/to/restore file2/to/restore
    

    The git checkout man page gives more information.

    If you want to revert to the commit before c5f567, append ~1 (where 1 is the number of commits you want to go back, it can be anything):

    git checkout c5f567~1 -- file1/to/restore file2/to/restore
    

    As a side note, I've always been uncomfortable with this command because it's used for both ordinary things (changing between branches) and unusual, destructive things (discarding changes in the working directory).

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

    And to revert to last committed version, which is most frequently needed, you can use this simpler command.

    git checkout HEAD file/to/restore
    
    0 讨论(0)
  • 2020-11-21 11:46
    git revert <hash>
    

    Will revert a given commit. It sounds like you think git revert only affects the most recent commit.

    That doesn't solve your problem, if you want to revert a change in a specific file and that commit changed more than that file.

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

    This worked for me:

    git checkout <commit hash> file
    

    Then commit the change:

    git commit -a
    
    0 讨论(0)
  • 2020-11-21 11:50

    Here's how rebase works:

    git checkout <my branch>
    git rebase master
    git checkout master
    git merge <my branch>
    

    Assume you have

    ---o----o----o----o  master
        \---A----B       <my branch>
    

    The first two commands ... commit git checkout git rebase master

    ... check out the branch of changes you want to apply to the master branch. The rebase command takes the commits from <my branch> (that are not found in master) and reapplies them to the head of master. In other words, the parent of the first commit in <my branch> is no longer a previous commit in the master history, but the current head of master. The two commands are the same as:

    git rebase master <my branch>
    

    It might be easier to remember this command as both the "base" and "modify" branches are explicit.

    . The final history result is:

    ---o----o----o----o   master
                       \----A'----B'  <my branch>
    

    The final two commands ...

    git checkout master
    git merge <my branch>
    

    ... do a fast-forward merge to apply all <my branch> changes onto master. Without this step, the rebase commit does not get added to master. The final result is:

    ---o----o----o----o----A'----B'  master, <my branch>
    

    master and <my branch> both reference B'. Also, from this point it is safe to delete the <my branch> reference.

    git branch -d <my branch>
    
    0 讨论(0)
  • 2020-11-21 11:50

    First Reset Head For Target File

    git reset HEAD path_to_file
    

    Second Checkout That File

    git checkout -- path_to_file
    
    0 讨论(0)
提交回复
热议问题