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?
I tried to look it up man git-diff | grep "line numbers"
and I tried googling but got nothing quickly.
git diff
There aren't currently any options to get line-numbers displayed vertically on the side with git diff
.
That information is available in the (c)hunk headers for each change in the diff though, it's just in unified-diff format:
@@ -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.
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 [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.
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.