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

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

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

    git prevision  
    

    where is the number of revisions of the file to rollback for file .
    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

提交回复
热议问题