How to get git diff with full context?

GitDiffGit Diff

Git Problem Overview


How to create patch suitable for reviewing in crucible?

git diff branch master --no-prefix > patch

This generates only 3 lines of context. So I do the following

git diff --unified=2000 branch master --no-prefix > patch

Hopefully all files will have less than 2000 lines. Is there a way to tell git to include all the lines in the file for patch without having to specify maximum lines?

Git Solutions


Solution 1 - Git

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.

Solution 2 - Git

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.

Solution 3 - Git

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.

Solution 4 - Git

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 https://stackoverflow.com/questions/26243145/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

Solution 5 - Git

This worked for me on macOS:

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

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

Solution 6 - Git

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionbalkiView Question on Stackoverflow
Solution 1 - Gitc24wView Answer on Stackoverflow
Solution 2 - GitEzraView Answer on Stackoverflow
Solution 3 - GitVonCView Answer on Stackoverflow
Solution 4 - GitYun WuView Answer on Stackoverflow
Solution 5 - GitNakilonView Answer on Stackoverflow
Solution 6 - GitstefcoView Answer on Stackoverflow