For “git diff” is there a -U option to show the whole file?

前端 未结 2 2033
挽巷
挽巷 2021-01-04 03:54

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 ge
相关标签:
2条回答
  • 2021-01-04 04:11

    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.

    0 讨论(0)
  • 2021-01-04 04:22

    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

    0 讨论(0)
提交回复
热议问题