Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get git diff with full context?

Tags:

git

git-diff

diff

People also ask

How do I see all git diff?

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 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.

What is git diff cached?

explainshell.com - git diff --cached. Show changes between the working tree and the index or a tree, changes between the index and a tree, changes between two trees, or changes between two files on disk.


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.


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.


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.


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

This worked for me on macOS:

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

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