Git diff with line numbers(Git log with line numbers)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24455377/
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
Git diff with line numbers (Git log with line numbers)
提问by Drew LeSueur
When I do a git diff
or a git log -p
, how do I get line numbers of the source file(s) inlined with the output?
当我执行 agit diff
或 a 时git log -p
,如何获得与输出内联的源文件的行号?
I tried to look it up man git-diff | grep "line numbers"
and I tried googling but got nothing quickly.
我试着查了一下man git-diff | grep "line numbers"
,我试着用谷歌搜索,但很快就一无所获。
采纳答案by Drew LeSueur
You can't get human-readable line numbers with git diff
您无法获得人类可读的行号 git diff
There aren't currently any options to get line-numbers displayed vertically on the side with git diff
.
目前没有任何选项可以让行号垂直显示在带有git diff
.
Unified-diff format
统一差异格式
That information is available in the (c)hunk headers for each change in the diff though, it's just in unified-diff format:
尽管差异中的每个更改都可以在 (c)hunk 标头中找到该信息,但它只是采用统一差异格式:
@@ -start,count +start,count @@
The original state of the file is represented with -
, and the new state is represented with +
(they don't mean additions and deletions in the hunk header. start
represents the starting line number of each version of the file, and count
represents how many lines are included, starting from the start point.
文件的原始状态用 表示-
,新状态用+
(它们不是指在大块头中增删改查。start
代表文件每个版本的起始行号,count
代表包含多少行) ,从起点开始。
Example
例子
diff --git a/osx/.gitconfig b/osx/.gitconfig
index 4fd8f04..fcd220c 100644
--- a/osx/.gitconfig
+++ b/osx/.gitconfig
@@ -11,7 +11,7 @@ <== HERE!
[color "branch"]
upstream = cyan
[color "diff"]
- meta = yellow
+ meta = cyan
plain = white dim
old = red bold
new = green bold
The hunk header
大块头
@@ -11,7 +11,7 @@
says that the previous version of the file starts at line 11, and includes 7 lines:
说文件的先前版本从第 11 行开始,包括 7 行:
11 [color "branch"]
12 upstream = cyan
13 [color "diff"]
14 - meta = yellow
14 + meta = cyan
15 plain = white dim
16 old = red bold
17 new = green bold
while the next version of the file also starts at line 11, and also includes 7 lines.
而下一个版本的文件也从第 11 行开始,并且也包括 7 行。
Unified-diff format isn't really for human consumption
统一差异格式并不适合人类消费
As you can probably tell, unified-diff format doesn't make it easy to figure out line numbers (at least if you're not a machine). If you really want line numbers that you can read, you'll need to use a diffing tool that will display them for you.
正如您可能会说的,统一差异格式并不容易找出行号(至少如果您不是机器的话)。如果你真的想要你可以阅读的行号,你需要使用一个比较工具来为你显示它们。
Additional Reading
补充阅读
回答by PFudd
Here's two more solutions, expanding on Andy Talkowski's code.
这是另外两个解决方案,扩展了 Andy Talkowski 的代码。
Plain text:
纯文本:
git diff | gawk 'match( git diff --color=always | \
gawk '{bare=# Massage the @@ counts so they are usable
function prep1() {
cat | awk -F',' 'BEGIN { convert = 0; }
/^@@ / { convert=1; }
/^/ { if ( convert == 1 ) { print ,,;
} else { print git config --global diff.tool vimdiff
;
}
convert=0;
}'
}
# Extract all new changes added with the line count
function prep2() {
cat | awk 'BEGIN { display=0; line=0; left=0; out=1;}
/^@@ / { out=0; inc=0; line=; line--; display=line; left=line; }
/^[-]/ { left++; display=left; inc=0; }
/^[+]/ { line++; display=line; inc=0; }
/^[-+][-+][-+] / { out=0; inc=0; }
/^/ {
line += inc;
left += inc;
display += inc;
if ( out == 1 ) {
print display,if &diff
set number
endif
;
} else {
print git difftool
;
}
out = 1;
inc = 1;
display = line;
}'
}
git diff | prep1 | prep2
;gsub("3[[][0-9]*m","",bare)};\
match(bare,"^@@ -([0-9]+),[0-9]+ [+]([0-9]+),[0-9]+ @@",a){left=a[1];right=a[2];next};\
bare ~ /^(---|\+\+\+|[^-+ ])/{print;next};\
{line=gensub("^(3[[][0-9]*m)?(.)","\2\1",1,git config --global diff.tool vimdiff
)};\
bare~/^-/{print "-"left++ ":" line;next};\
bare~/^[+]/{print "+"right++ ":" line;next};\
{print "("left++","right++"):"line;next}'
,"^@@ -([0-9]+),[0-9]+ [+]([0-9]+),[0-9]+ @@",a){left=a[1];right=a[2];next};\
/^(---|\+\+\+|[^-+ ])/{print;next};\
{line=substr(if &diff
set number
endif
,2)};\
/^-/{print "-" left++ ":" line;next};\
/^[+]/{print "+" right++ ":" line;next};\
{print "(" left++ "," right++ "):"line}'
Colored text, assuming \033[66m
is the format for color codes:
彩色文本,假设\033[66m
是颜色代码的格式:
git difftool
The code changes lines that start with -
and +
to -1:-
and +1:+
, respectively, and lines that start with nothing to
(5,6):
. The numbers are the line numbers from the respective file.
代码分别将以-
和开头的行更改+
为-1:-
和+1:+
,以及将不以任何内容开头的行更改为
(5,6):
。这些数字是来自相应文件的行号。
回答by Andy Talkowski
Here is a script that attempts to fix this - not tested it in anger but it seems ok. It relies on the records git diff produces and uses awk to maintain line counts.
这是一个尝试解决此问题的脚本 - 没有在愤怒中对其进行测试,但似乎没问题。它依赖于 git diff 生成的记录并使用 awk 来维护行数。
git diff # default
@@ -10,8 +10,8 @@
回答by wisbucky
You can use git difftool
to do the diff with an external editor that will display line numbers. Here's how to do it with vim / vimdiff:
您可以使用git difftool
将显示行号的外部编辑器进行比较。以下是如何使用 vim / vimdiff 执行此操作:
Set vimdiff as git's difftool:
git diff -U0 @@ -13,2 +13,2 @@
Configure
~/.vimrc
to automatically show line numbers when using vimdiff:git blame
Run git difftool, which will use vimdiff with line numbers:
#include <stdio.h> int main() { printf("Hello World\n"); return 0; }
将 vimdiff 设置为 git 的 difftool:
git add hello_world.c git commit -m "add hello_world.c"
配置
~/.vimrc
为在使用 vimdiff 时自动显示行号:// Basic hello world example #include <stdio.h> int main(int argc, char *argv[]) { printf("Hello Gabriel\n"); int i = 700; printf("i = %i\n", i); return 0; }
运行 git difftool,它将使用带有行号的 vimdiff:
git diff
回答by wisbucky
A quick way is to use git diff -U0
. That will set the lines of context to 0, which will make the @@ values match the actual changed lines. By default, the @@ values include 3 lines of before/after context, which is not convenient for humans.
一个快速的方法是使用git diff -U0
. 这会将上下文行设置为 0,这将使 @@ 值与实际更改的行匹配。默认情况下,@@ 值包括 3 行 before/after 上下文,这对人类来说是不方便的。
Example:
例子:
$ git diff
diff --git a/hello_world.c b/hello_world.c
index e01704a..e971b73 100644
--- a/hello_world.c
+++ b/hello_world.c
@@ -1,8 +1,12 @@
+// Basic hello world example
+
#include <stdio.h>
-int main()
+int main(int argc, char *argv[])
{
- printf("Hello World\n");
-
+ printf("Hello Gabriel\n");
+
+ int i = 700;
+ printf("i = %i\n", i);
return 0;
-}
\ No newline at end of file
+}
This is hard to calculate the line numbers of the changed lines because line 10 refers to the first line of the before context. The actual line number of the first changed line is 10+3=13. To calculate the number of changed lines, then you have to also subtract the before and after context: 8-3-3=2.
这很难计算更改行的行号,因为第 10 行指的是 before 上下文的第一行。第一个更改的行的实际行号为 10+3=13。要计算更改的行数,还必须减去前后上下文:8-3-3=2。
$ git diffn
diff --git a/hello_world.c b/hello_world.c
index e01704a..e971b73 100644
--- a/hello_world.c
+++ b/hello_world.c
@@ -1,8 +1,12 @@
+ 1:+// Basic hello world example
+ 2:+
1, 3: #include <stdio.h>
2, 4:
- 3 :-int main()
+ 5:+int main(int argc, char *argv[])
4, 6: {
- 5 :- printf("Hello World\n");
- 6 :-
+ 7:+ printf("Hello Gabriel\n");
+ 8:+
+ 9:+ int i = 700;
+ 10:+ printf("i = %i\n", i);
7, 11: return 0;
- 8 :-}
\ No newline at end of file
+ 12:+}
As you can see, setting context = 0 makes the @@ values easier for humans to read. You can see that the changed lines start at line 13, and there are 2 changed lines.
如您所见,设置 context = 0 使 @@ 值更易于人类阅读。可以看到更改的行从第 13 行开始,有 2 行更改。
This isn't perfect, since it only shows the line number for each block. If you want to see line numbers for every line, then use difftool for an external editor. See https://stackoverflow.com/a/50049752
这并不完美,因为它只显示每个块的行号。如果您想查看每一行的行号,请使用 difftool 作为外部编辑器。见https://stackoverflow.com/a/50049752
回答by Gabriel Staples
I like to use git difftool
with meldas my difftool. It's easier to look at than git diff
, has a nice side-by-side gui comparison, and shows line numbers on each side.
我喜欢用git difftool
与MELD作为我difftool。它比 更容易查看git diff
,有很好的并排 gui 比较,并在每一侧显示行号。
Setup:
设置:
Sample Screenshot:
示例截图:
Update 24 May 2020:
2020 年 5 月 24 日更新:
I just wrote git diffn
over the last few days to be a drop-in replacement for git diff
on the command-line. Give it a shot. See my other answer here.
我刚刚git diffn
在过去几天写了git diff
作为命令行的替代品。试一试。在此处查看我的其他答案。
回答by Juan
You can try
你可以试试
git config --global color.diff.meta "blue"
git config --global color.diff.old "black red strike"
git config --global color.diff.new "black green italic"
git config --global color.diff.context "yellow bold"
on the file. It shows you the committer, commit id, and line number for each line in the file.
在文件上。它向您显示文件中每一行的提交者、提交 ID 和行号。
回答by Gabriel Staples
As of 24 May 2020, you can now use the third-party tool git diffn
for this purpose. It's a light-weight wrapper around git diff
, written in the awk
pattern/action-based programming language. Here's a sample output from running git diffn
:
自 2020 年 5 月 24 日起,您现在可以git diffn
为此目的使用第三方工具。它是围绕 的轻量级包装器git diff
,以awk
基于模式/动作的编程语言编写。这是运行的示例输出git diffn
:
Here's a demo:
这是一个演示:
1/3: Demo of git diffn
:
1/3:演示git diffn
:
Create this file:
创建此文件:
hello_world.c:
hello_world.c:
git diffn --color=never HEAD~
git diffn --no-color HEAD~3..HEAD~2
Commit it:
提交它:
git config --global color.diff.meta "blue"
git config --global color.diff.old "black red strike"
git config --global color.diff.new "black green italic"
git config --global color.diff.context "yellow bold"
Change it to this and save the file:
将其更改为此并保存文件:
hello_world.c:
hello_world.c:
git diffn --color=never HEAD~
git diffn --no-color HEAD~3..HEAD~2
Now run:
现在运行:
git clone https://github.com/ElectricRCAircraftGuy/eRCaGuy_dotfiles.git
cd eRCaGuy_dotfiles/useful_scripts
mkdir -p ~/bin
ln -si "${PWD}/git-diffn.sh" ~/bin/git-diffn
Here's the output of git diff
first for comparison purposes:
这是git diff
出于比较目的的first的输出:
mkdir -p ~/bin
cd ~/bin
wget https://raw.githubusercontent.com/ElectricRCAircraftGuy/eRCaGuy_dotfiles/master/useful_scripts/git-diffn.sh
chmod +x git-diffn.sh
mv git-diffn.sh git-diffn
And a screenshot to show the color. Note that the red highlighted section is simply showing empty whitespace (spaces in this case) that could be deleted:
以及显示颜色的屏幕截图。请注意,红色突出显示的部分只是显示可以删除的空白空格(在这种情况下为空格):
Now here's the output of git diffn
. Notice it shows all line numbers perfectly!
现在这是git diffn
. 请注意,它完美地显示了所有行号!
- Line numbers for deleted linesare on the left, and show a
-
sign on both the far left AND to the right of the:
to help you see better--whether your eyes like to scan down to the right of the colon or down on the far left of the screen. - Line numbers for added linesare farther to the right, and show a
+
sign on both the far left AND to the right of the:
. - Line numbers for unchanged linesshown for context are shown for both the left (old file) AND the right (new file), separated by a
,
.
- 已删除行的行号在左侧,并
-
在最左侧和最右侧显示一个符号,:
以帮助您看得更清楚——无论您的眼睛喜欢向下扫描到冒号的右侧还是向下扫描到最远的地方屏幕左侧。 - 添加的行的行号更靠右,并
+
在:
. - 左侧(旧文件)和右侧(新文件)显示上下文中未更改行的行号,以
,
.分隔。
Output of git diffn
:
的输出git diffn
:
git config --global diff.tool meld
And a screenshot to show the color. Notice that the colons are NOT colored or stylized to match the surrounding text on the left and right. This is intentionaland designed-in behavior to act as a visual separator between the line numbers added on the left and the original git diff
output on the right.
以及显示颜色的屏幕截图。请注意,冒号没有着色或样式化以匹配左侧和右侧的周围文本。这是有意和设计的行为,用作左侧添加的行号和git diff
右侧的原始输出之间的视觉分隔符。
2/3: What is it?
2/3:是什么?
From the top of git-diffn.sh
:
DESCRIPTION:
描述:
git-diffn.sh
git-diffn.sh
- a drop-in replacement for
git diff
which also shows line 'n'umbers! Use it exactlylikegit diff
, except you'll see these beautiful line numbers as well to help you make sense of your changes. - since it's just a light-weight awk-language-based wrapper around
git diff
, it accepts ALL options and parameters thatgit diff
accepts. Examples:git diffn HEAD~
git diffn HEAD~3..HEAD~2
works with any of your
git diff
color settings, even if you are using custom colors- See my answer here for how to set custom diff colors, as well as to see a screenshot of custom-color output from
git diffn
: How do you customize the color of the diff header in git diff? Here are some sample
git config
commands from my answer above to set customgit diff
colors and attributes (text formatting):git difftool -y config.rb
- See my answer here for how to set custom diff colors, as well as to see a screenshot of custom-color output from
in
##代码##git diffn
, color output is ON by default; if you want to disable the output color, you must use--no-color
or--color=never
. Seeman git diff
for details. Examples:
- 一个插入式替换
git diff
也显示行'n'numbers!完全像 一样使用它git diff
,除了您还会看到这些漂亮的行号以帮助您理解所做的更改。 - 因为它只是一个基于 awk 语言的轻量级包装器
git diff
,它接受所有接受的选项和参数git diff
。例子:git diffn HEAD~
git diffn HEAD~3..HEAD~2
适用于您的任何
git diff
颜色设置,即使您使用的是自定义颜色- 有关如何设置自定义差异颜色以及查看自定义颜色输出的屏幕截图,请参阅我的答案
git diffn
:如何在 git diff 中自定义差异标头的颜色? 以下是
##代码##git config
我上面回答中的一些示例命令,用于设置自定义git diff
颜色和属性(文本格式):
- 有关如何设置自定义差异颜色以及查看自定义颜色输出的屏幕截图,请参阅我的答案
in
##代码##git diffn
,颜色输出默认开启;如果要禁用输出颜色,则必须使用--no-color
或--color=never
。详情请参阅man git diff
。例子:
3/3: Installation
3/3:安装
- Windows (untested): this may work inside the bash terminal that comes with Git for Windows, but is untested. Install Git for Windows. Open the bash terminal it comes with, and try to follow the instructions below. I need some testers who will test this in Git for Windows. Please see and answer here: https://github.com/git-for-windows/git/issues/2635.
- Mac (untested): use the terminal and follow the instructions below. You may need to install
gawk
. If so, try this:brew install gawk
. - Linux (tested on Ubuntu 18.04 and works perfectly): follow the terminal instructions below.
- Windows(未经测试):这可能在Git for Windows附带的 bash 终端内工作,但未经测试。安装适用于 Windows 的 Git。打开它附带的 bash 终端,并尝试按照以下说明进行操作。我需要一些测试人员,他们将在 Windows 版 Git 中对此进行测试。请在此处查看并回答:https: //github.com/git-for-windows/git/issues/2635。
- Mac(未经测试):使用终端并按照以下说明进行操作。您可能需要安装
gawk
. 如果是这样,试试这个:brew install gawk
。 - Linux(在 Ubuntu 18.04 上测试并完美运行):按照下面的终端说明进行操作。
Option 1 (my recommendation):download the whole repo and then create a symlink to the program so that you can easily receive updates by doing a git pull
from the repo whenever you want.
选项 1(我的建议):下载整个 repo,然后创建程序的符号链接,以便您可以随时通过git pull
repo执行 a 来轻松接收更新。
First, cd
to wherever you want to install this. Then run:
首先,cd
到任何你想安装它的地方。然后运行:
Done! Now just do the final step below!
完毕!现在只需执行下面的最后一步!
Option 2 (for those who just want the 1 file):download just the one file one time.
选项 2(对于那些只想要 1 个文件的人):一次只下载一个文件。
##代码##Done! Now just do the final step below!
完毕!现在只需执行下面的最后一步!
Final step:
最后一步:
Now close and re-open your terminal, or re-source it with . ~/.bashrc
, and you are done!
现在关闭并重新打开您的终端,或者使用 重新获取它. ~/.bashrc
,您就完成了!
git diffn
will now work as an exact drop-in replacement for git diff
!
git diffn
现在将作为git diff
!
回答by HaxtraZ
First, config your git diff tool, e.g. Meld
首先,配置你的 git diff 工具,例如 Meld
##代码##Then, yank your difftool on some file:
然后,将 difftool 拉到某个文件上:
##代码##Remember setting line number in your diff tool's preference.
请记住在差异工具的首选项中设置行号。