how can I list all the different versions of a file, and diff them also?

前端 未结 4 1363
慢半拍i
慢半拍i 2020-12-15 04:29

using git, I want to list all the different revisions of a given file.

Then, I want to choose a particular version and compare it when another.

How can I do

4条回答
  •  囚心锁ツ
    2020-12-15 04:54

    You can use a script like this to dump all the versions of a file to a separate file:

    e.g.

    git_all_versions_of path/to/somefile.txt
    

    It will generate a bunch of files in the same folder as the original file, named like the following, with the most recent change postfixed with "1". Note that it will also dump another file ending in .logmsg that has the log message of the commit as well.

    path/to/somefile.txt.1.0dea419
    path/to/somefile.txt.1.0dea419.logmsg
    path/to/somefile.txt.2.cdea8s9
    path/to/somefile.txt.2.cdea8s9.logmsg
    path/to/somefile.txt.3.fdsf2d
    path/to/somefile.txt.3.fdsf2d.logmsg
    etc...
    

    After I've dumped all the files, I just run a grep -r DELETED_METHOD_NAME somefile.txt.* to find what I'm looking for.

    git_all_versions_of

    #!/bin/sh
    if [ "$#" -ne 1 ] || [ "$1" == "help" ]
    then
      echo "dump all git versions of a file to separate files"
      echo 
      echo "usage: $0 FILENAME";
      echo 
      echo "e.g."
      echo 
      echo "$ $0 path/to/somefile.txt"
      echo 
      echo "path/to/somefile.txt.1.0dea419"
      echo "path/to/somefile.txt.1.0dea419.logmsg"
      echo "path/to/somefile.txt.2.cdea8s9"
      echo "path/to/somefile.txt.2.cdea8s9.logmsg"
      echo "path/to/somefile.txt.3.fdsf2d"
      echo "path/to/somefile.txt.3.fdsf2d.logmsg"
      echo "..."
      exit 1
    fi
    
    index=1
    for commit in $(git log --pretty=format:%h "$1")
    do
      padindex=$(printf %03d "$index")
      out="$1.$padindex.$commit"
      log="$out.logmsg"
      echo "saving version $index to file $out for commit $commit"
      echo "*******************************************************" > "$log"
      git log -1 --pretty=format:"%s%nAuthored by %an at %ai%n%n%b%n" "$commit" >> "$log"
      echo "*******************************************************" >> "$log"
      git show "$commit:./$1" > "$out"
      let index++
    done
    

提交回复
热议问题