mrblah
mrblah

Reputation: 103677

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

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?

Upvotes: 51

Views: 33205

Answers (4)

Brad Parks
Brad Parks

Reputation: 72271

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

Upvotes: 34

hagello
hagello

Reputation: 3265

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.

Upvotes: 17

RyanWilcox
RyanWilcox

Reputation: 13972

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.

Upvotes: 4

daf
daf

Reputation: 5250

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.

Upvotes: 52

Related Questions