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

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

    I have to plug EasyGit here, which is a wrapper to make git more approachable to novices without confusing seasoned users. One of the things it does is give more meanings to git revert. In this case, you would simply say:

    eg revert foo/bar foo/baz

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

    Use git log to obtain the hash key for specific version and then use git checkout <hashkey>

    Note: Do not forget to type the hash before the last one. Last hash points your current position (HEAD) and changes nothing.

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

    If you know how many commits you need to go back, you can use:

    git checkout master~5 image.png
    

    This assumes that you're on the master branch, and the version you want is 5 commits back.

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

    git-aliases, awk and shell-functions to the rescue!

    git prevision <N> <filename>
    

    where <N> is the number of revisions of the file to rollback for file <filename>.
    For example, to checkout the immediate previous revision of a single file x/y/z.c, run

    git prevision -1 x/y/z.c
    

    How git prevision works?

    Add the following to your gitconfig

    [alias]
            prevision = "!f() { git checkout `git log --oneline $2 |  awk -v commit="$1" 'FNR == -commit+1 {print $1}'` $2;} ;f"
    

    The command basically

    • performs a git log on the specified file and
    • picks the appropriate commit-id in the history of the file and
    • executes a git checkout to the commit-id for the specified file.

    Essentially, all that one would manually do in this situation,
    wrapped-up in one beautiful, efficient git-alias - git-prevision

    0 讨论(0)
  • 2020-11-21 11:40
    git checkout -- foo
    

    That will reset foo to HEAD. You can also:

    git checkout HEAD^ foo
    

    for one revision back, etc.

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

    In order to go to a previous commit version of the file, get the commit number, say eb917a1 then

    git checkout eb917a1 YourFileName
    

    If you just need to go back to the last commited version

    git reset HEAD YourFileName
    git checkout YourFileName
    

    This will simply take you to the last committed state of the file

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