How do I manually check the difference between two git commits?

847 Views Asked by At

How do I manually check the difference of files between two commits in the same git folder.

There's a ton of posts here how to configure git to use kdiff3 as the default merge tool but this question relates to manually attaching directories so that I can compare differences between commits.

To address the comments below:

Assume I have a file with filepath /path/to/dir/filename.txt. /path/to/dir/ is a git directory (was instantiated with git init).

I am trying to view the difference of /path/to/dir/filename.txt at commitid=bacfa3 and /path/to/dir/filename.txt at commitid=aafaf8. I would prefer not to use git's command line interface git diff to perform this but rather view these changes only using the kdiff3 UI.

To address the additional answer below:

I attached an image to the kdiff3 UI. There is an option to add files and a directory. Is it possible to select a file /path/to/dir/filename.txt at a specific commit (bacfa3) and the same file at another commit (aafaf8) and compare the two files using just the kdiff3 UI (no command line interface).

1

There are 1 best solutions below

2
On BEST ANSWER

You can use git difftool :

# from /path/to/dir/ :
git difftool bacfa3 aafaf8 -- filename.txt

In cases when you want to compare several files, or open your diff viewer in directory comparison mode, add the -d option :

git difftool -d bacfa3 aafaf8
git difftool -d bacfa3 aafaf8 -- some/dir
# the following will compare the 2 commits, restricting the compared files
# to only the files named on the command line :
git difftool -d bacfa3 aafaf8 -- file1.txt file2.txt path/to/file3.txt

note : the -- in the commands above is mostly optional, it is the standard way to say "what comes afterwards is not a branch name, or a tag name, or a commit ref, or a command line option (if a file starts with - for exampel), it's only paths".
When there is no ambiguity, git knows just fine what to look for with git difftool bacfa3 Readme.md.

To get kdiff3 as the standard viewer : Configuring kdiff3 with git


The basic use of git difftool is : any set of options and arguments understood by git diff(*) will also be understood by git difftool and git difftool -d

(*) okay, there may be one or two exceptions, such as --word-diff or --histogram, because git doesn't control anymore how the diff viewer behaves.


[edit] to answer the "can I do this from kdiff3 GUI only ?" point :

I don't think you can from kdiff3 alone (unless you see a "VCS" entry somewhere in the menus ?), look into GUI frontends to git :

There is a good list of GUI tools on the official git site :

I had a good experience with Git extensions (if you are running Windows), gitk is developped very closely with git, and other tools are quite known, such as Gitkraken, Sourcetree ...

From such tools, you will have a GUI to select the commits and files you want to compare, and they all have an "open in [graphical diff viewer] ..." action.