Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Level 29 - there is no way to get the line number #252

Open
ray-mints opened this issue Feb 15, 2019 · 2 comments
Open

Level 29 - there is no way to get the line number #252

ray-mints opened this issue Feb 15, 2019 · 2 comments

Comments

@ray-mints
Copy link

After using git diff or gitk to see changes and running githug command once again there is a question:

What is the number of the line which has changed?

According to this stackoverflow answer there's no way for git diff to show a line number.
It is not clear that user should use another software (like vim) to get the answer.

@duianto
Copy link
Contributor

duianto commented Feb 15, 2019

I solved it by reading up on how the diff chunks (hunks?) header works.

For example with a committed file (test.txt) containing these lines:

one
two
three
four
five
six
seven
eight
nine
ten

After changing line five to changed, saving the file, and calling git diff, it looks like this:

diff --git a/test.txt b/test.txt
index c9e9e05..796305d 100644
--- a/test.txt
+++ b/test.txt
@@ -2,7 +2,7 @@ one
 two
 three
 four
-five
+changed
 six
 seven
 eight

The lines:

--- a/test.txt
+++ b/test.txt

means that the changes in file a/test.txt (the committed changes) are marked with a - (dash) sign,
and the changes in file b/test.txt (the not yet committed changes) are marked with a + sign.

The diff chunks header @@ -2,7 +2,7 @@ says that:

  • @@ are just chunk header symbols, probably to make the chunk headers more visible
  • -2 means that the first line that's shown from a/test.txt is two
  • 7 means that 7 lines are shown from file a/test.txt
  • +2 means that the first line from from b/test.txt also is two
  • 7 lines are also shown from file b/test.txt

That means that the first line that's changed is line 5 because the visible chunk starts on line 2 two and if we count down we get to -five which is the first changed line in file a/test.txt. Which is the fifth line.

The context lines before and after the changed lines can be removed. A comment https://stackoverflow.com/a/50049834 on the SO (Stack Overflow) page that you linked to, says:

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 outputs:

diff --git a/test.txt b/test.txt
index c9e9e05..796305d 100644
--- a/test.txt
+++ b/test.txt
@@ -5 +5 @@ four
-five
+changed

No context (extra lines) are shown above and below, and the chunk header lists the changed line number
-5 +5 (line 5 in both a/test.txt and b/test.txt).

These two pages (they were among the first hits when searching for git diff) laid out the basics:
https://www.git-tower.com/learn/git/ebook/en/command-line/advanced-topics/diffs
https://www.atlassian.com/git/tutorials/saving-changes/git-diff
but it was most helpful to do some experiments (what happens if I do this...).

This might also be useful reading: https://en.wikipedia.org/wiki/Diff

Hopefully this was of some help.

@wiiikiii
Copy link

wiiikiii commented Nov 8, 2024

with git diff -U0 the line number will be displayed right

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants