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

4

48

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 this?

Yordan answered 26/12, 2009 at 19:27 Comment(0)
E
50

To show a history of changes to a particular file, you can use git log:

git log -p -- path/to/file

The -p tells it to show the diff between each revision and its parent. To get a cumulative diff between two revisions, take the ID of the two revisions, and pass them to git diff:

git diff abc123 def456 -- path/to/file.

Where abc123 and def456 are the revision IDs.

Enyedy answered 26/12, 2009 at 20:23 Comment(1)
-p stands for --patch for the curious—also, why is git log -h so short and unhelpful? 😅Babylon
D
32

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
Divorcee answered 29/9, 2015 at 16:24 Comment(5)
Also git_log_short [git_log_message_for_commit & choose_col] are not foundLasso
No, the function git_log_short is not defined anywhere in this script.Gavrielle
Life saver! Git foo is here.Schwerin
I fixed the above errors mentioned by Wolfgang and Alex, and have included the fixes in the scriptDivorcee
HUGE +1 for providing a reasonably documented end-to-end script as an answer to a SO question. THANKS !!!!Ilyssa
N
16

Use the standard GUI tool of git:

 gitk --all -- path/to/file

It displays the version history stripped down to the commits that affect path/to/file.

As display mode for the comparison between the current (new) version and the preceding (old) version you can choose:

  • old version or
  • new version or
  • diff

Additionally, you can select a commit and then right click another commit and choose Diff this -> selected in the context menu. Everything that you wished for.

Nicks answered 5/10, 2015 at 5:7 Comment(0)
E
4

I wrote a tool that would get you most of the way there (printing out the entire contents of a file, as it was in SHA-1-WHATEVER.

git-cat

You could either put a little shell script over that to do everything automatically, or the README.markdown file in that repository also gives references to where I learned all the stuff I needed to write the command.

Emden answered 26/12, 2009 at 21:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.