How to get git diff with full context?

前端 未结 6 747
暗喜
暗喜 2021-01-30 07:48

How to create patch suitable for reviewing in crucible?

git diff branch master --no-prefix > patch

This generates only 3 lines of context. S

相关标签:
6条回答
  • 2021-01-30 08:14

    Note: git1.8.1rc1 announce (December 8th, 2012) includes:

    A new configuration variable "diff.context" can be used to give the default number of context lines in the patch output, to override the hardcoded default of 3 lines.

    so that could help, here, generate a more complete context.

    0 讨论(0)
  • 2021-01-30 08:16

    I know this is old, but I also dislike hard-coded solutions, so I tested this:

    git diff -U$(wc -l MYFILE)
    

    Using -U seems to be the only way to approach the issue, but using a line count promises that it will work for even a small change in a very large file.

    0 讨论(0)
  • 2021-01-30 08:16

    This seems to work pretty nicely:

    git diff --no-prefix -U1000
    

    With the caveat:

    The -U flag specifies lines of context. You might need to increase this if there are more than 1000 lines between your changes.

    0 讨论(0)
  • 2021-01-30 08:30

    Got inspiration and so I added a git alias.

    $ cat ~/.gitconfig | fgrep diff
            df = "!git diff -U$(wc -l \"$1\" | cut -d ' ' -f 1) \"$1\""
    $ git df <file>
    

    Update:

    Just found "git df" does not work sometimes, due to directory change when executing git alias. (See git aliases operate in the wrong directory). So this is the updated version:

    $ cat ~/.gitconfig | fgrep df
            df = "! [ \"$GIT_PREFIX\" != \"\" ] && cd \"$GIT_PREFIX\"; ~/bin/git_df.sh"
    $ 
    $ cat ~/bin/git_df.sh
    #!/bin/bash
    for FILE in $@; do
        git diff -U$(wc -l "${FILE}" | cut -d ' ' -f 1) "${FILE}"
    done
    exit 0
    
    0 讨论(0)
  • 2021-01-30 08:33

    Previously accepted solutions don't work for me when viewing a specific file/commit (the -U option seems to mess with rev/path parsing), but --inter-hunk-context= works in this case on git version 2.24.0:

    git diff \
        --no-prefix \
        --inter-hunk-context=2000 \
        master -- \
            path/to/file.py
    

    If you don't know the file size, you can of course find it with wc -l instead of hard-coding it:

    git diff \
        --no-prefix \
        --inter-hunk-context=$(wc -l path/to/file.py) \
        master -- \
            path/to/file.py
    
    0 讨论(0)
  • 2021-01-30 08:37

    This worked for me on macOS:

    git diff -U$(wc -l main.htm | xargs)
    

    see "How to trim whitespace from a Bash variable?"

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