Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I view the output of `git show` in a diff viewer like meld, kdiff3, etc

There are many SO questions that show how to view the output of a git diff command in a diff viewer like meld using git difftool or otherwise. I am not asking about git diff though.

I want to see the output of git show <previous commit sha1> in a diff viewer like meld. How can I do this?

like image 558
Buttons840 Avatar asked Jul 09 '13 21:07

Buttons840


People also ask

How do I view a git diff file?

The diff can be done with git diff (followed by the filename or nothing if you want to see the diff of all modified files). But if you already did something like git add * , you have to undo with git restore --staged .

What diff tool does git use?

git difftool is a Git command that allows you to compare and edit files between revisions using common diff tools. git difftool is a frontend to git diff and accepts the same options and arguments.


2 Answers

You can use git difftool to show a single commit.

Say you want to see the commit with the sha1 abc123:

git difftool abc123~1 abc123 

(~1 tells git to move to the previous commit, so abc123~1 is the commit before abc123)

If you use this regularly, you could make a custom git command to make it easier:

  1. Create a file called git-showtool somewhere on your $PATH with the following contents:

    git difftool $1~1 $1 
  2. Give that file execute permissions:

    chmod +x ~/path/to/git-showtool 
  3. Use the command git showtool <sha1 or tag or ...>

  4. Profit.
like image 172
georgebrock Avatar answered Oct 07 '22 04:10

georgebrock


Building on georgebrock's response, you can create an alias in .gitconfig, something like this:

showtool = "!showci () { rev=${1:-HEAD}; git difftool $rev~1 $rev; }; showci $1" 

Then you can run it with git showtool abc123 (without needing to create a separate shell script for this). If you leave out the revision it will default to HEAD.

like image 31
sagittarian Avatar answered Oct 07 '22 04:10

sagittarian