如何获得具有完整上下文的 git diff?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13627598/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get git diff with full context?
提问by balki
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
这仅生成 3 行上下文。所以我做以下
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?
希望所有文件都少于 2000 行。有没有办法告诉 git 将文件中的所有行都包含在补丁中,而不必指定最大行数?
采纳答案by Ezra
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.
使用 -U 似乎是解决这个问题的唯一方法,但是使用行数承诺它甚至可以用于非常大的文件中的微小更改。
回答by c24w
回答by VonC
Note: git1.8.1rc1 announce (December 8th, 2012)includes:
注意:git1.8.1rc1 公告(2012 年 12 月 8 日)包括:
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.
新的配置变量“
diff.context
”可用于在补丁输出中提供默认的上下文行数,以覆盖 3 行的硬编码默认值。
so that could help, here, generate a more complete context.
这样可以帮助生成更完整的上下文。
回答by Yun Wu
Got inspiration and so I added a git alias.
得到了灵感,所以我添加了一个 git 别名。
$ cat ~/.gitconfig | fgrep diff
df = "!git diff -U$(wc -l \"\" | cut -d ' ' -f 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:
刚刚发现“git df”有时不起作用,因为执行 git alias 时目录更改。(参见git aliases 在错误目录中操作)。所以这是更新的版本:
$ 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
回答by Nakilon
This worked for me on macOS:
这在 macOS 上对我有用:
git diff -U$(wc -l main.htm | xargs)
回答by stefco
Previously accepted solutions don't work for me when viewing a specificfile/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
:
以前接受的解决方案在查看特定文件/提交时对我不起作用(该-U
选项似乎与 rev/path 解析混淆),但--inter-hunk-context=
在这种情况下适用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:
如果您不知道文件大小,当然可以使用wc -l
而不是硬编码来找到它:
git diff \
--no-prefix \
--inter-hunk-context=$(wc -l path/to/file.py) \
master -- \
path/to/file.py