Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

For "git diff" is there a -U<infinity> option to show the whole file?

Tags:

git

I need to generate a full-context git diff programmatically for a web ui.

A CLI for generating a full-context diff was covered in questions:

  • How to get git diff with full context?
  • Git show whole file changes

The prevalent answer is something like git diff -U99999

With a -U / --unified option with a ridiculously high threshold (e.g. 999,999), doing git diff -U999999:

  1. Makes me suspect that there could be a performance hit
  2. Even worse, it's a correctness issue if my file is larger than 1M lines

Is there a -U option to show the whole file?

like image 298
Aleksandr Levchuk Avatar asked Feb 25 '15 19:02

Aleksandr Levchuk


People also ask

Does git store diffs or whole files?

No, commit objects in git don't contain diffs - instead, each commit object contains a hash of the tree, which recursively and completely defines the content of the source tree at that commit.

What does git diff show you?

The git diff command helps you see, compare, and understand changes in your project. You can use it in many different situations, e.g. to look at current changes in your working copy, past changes in commits, or even to compare branches.

What does ++ mean in git diff?

When viewing a combined diff, if the two files you're comparing have a line that's different from what they were merged into, you will see the ++ to represent: one line that was added does not appear in either file1 or file2.


2 Answers

If you just use a large number with -U, you could choose the large number to be the point at which your application can't handle displaying such a large file (diff).

it's a correctness issue if my file is larger than 1M lines

And to address this issue, you can check the output for more than one @@ ... @@ line to determine whether it's complete — this allows you to avoid silently giving a wrong number.

like image 155
Kevin Reid Avatar answered Sep 28 '22 05:09

Kevin Reid


Frankly, the best option is to use git difftool rather than vanilla git diff. To see which tools your version of git supports, enter

git difftool --tool-help

which, with my version (2.3.0), shows the following

$ git difftool --tool-help
'git difftool --tool=<tool>' may be set to one of the following:
        araxis
        gvimdiff
        gvimdiff2
        gvimdiff3
        meld
        vimdiff
        vimdiff2
        vimdiff3

The following tools are valid, but not currently available:
        bc
        bc3
        codecompare
        deltawalker
        diffmerge
        diffuse
        ecmerge
        emerge
        kdiff3
        kompare
        opendiff
        p4merge
        tkdiff
        xxdiff

I usually use meld, but that's just a personal preference. git difftool takes the same arguments as git diff plus a few to help with the process (I find -y useful to prevent the prompts when moving from one file to the next).

To check out the changes introduced by a specific commit, for example, you can use

git difftool -y -t meld 08f0f82^..08f0f82

obviously replacing 08f0f82 with the correct SHA-1.

My biggest complaint is that it launches the tool for each modified file in sequence (hence specifying the -y option).

If you only wanted to examine the changes to a particular file in that commit, you can just add the filename to the command line.

git difftool -y -t meld 08f0f82^..08f0f82 myfile.c

Obviously, this is for interactive use - not for scripting

like image 36
kdopen Avatar answered Sep 28 '22 04:09

kdopen