How to get git diff with full context?

隐身守侯 提交于 2019-12-03 02:06:36

问题


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?


回答1:


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.




回答2:


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.




回答3:


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.




回答4:


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



回答5:


This worked for me on Mac OS:

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

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



来源:https://stackoverflow.com/questions/13627598/how-to-get-git-diff-with-full-context

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!