Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should I use git diff for long lines?

People also ask

How do I use the diff command in git?

You can run the git diff HEAD command to compare the both staged and unstaged changes with your last commit. You can also run the git diff <branch_name1> <branch_name2> command to compare the changes from the first branch with changes from the second branch. Order does matter when you're comparing branches.

What does ++ mean in git diff?

When viewing a combined diff, if the two files you're comparing have a line that's different from what they were merged into, you will see the ++ to represent: one line that was added does not appear in either file1 or file2.

Is git diff useful?

Diff command is used in git to track the difference between the changes made on a file. Since Git is a version control system, tracking changes are something very vital to it. Diff command takes two inputs and reflects the differences between them. It is not necessary that these inputs are files only.

How do I continue git diff?

While in git diff , simply hit n to go straight to the next file, and again to the one afterwards, and so on. You can also use N to go back a file. (For these commands to work, you'll need to first type /^diff and press Enter , as explained in this answer.) Pressing n finds the next search term.


Or if you use less as default pager just type -S while viewing the diff to reenable wrapping in less.


The display of the output of git diff is handled by whatever pager you are using.

Commonly, under Linux, less would be used.

You can tell git to use a different pager by setting the GIT_PAGER environment variable. If you don't mind about paging (for example, your terminal allows you to scroll back) you might try explicitly setting GIT_PAGER to empty to stop it using a pager. Under Linux:

$ GIT_PAGER='' git diff

Without a pager, the lines will wrap.

If your terminal doesn't support coloured output, you can also turn this off using either the --no-color argument, or putting an entry in the color section of your git config file.

$ GIT_PAGER='' git diff --no-color

You can also use git config to setup pager to wrap.

$ git config core.pager 'less -r' 

Sets the pager setting for the current project.

$ git config --global core.pager 'less -r' 

Sets the pager globally for all projects


With full credit to Josh Diehl in a comment to this answer, I nevertheless feel like this ought to be an answer unto itself, so adding it:

One way to deal with seeing differences in long lines is to use a word-oriented diff. This can be done with:

git diff --word-diff

In this case, you'll get a significantly different diff output, that shows you specifically what has changed within a line.

For example, instead of getting something like this:

diff --git a/test-file.txt b/test-file.txt
index 19e6adf..eb6bb81 100644
--- a/test-file.txt
+++ b/test-file.txt
@@ -1 +1 @@
-this is a short line
+this is a slightly longer line

You might get something like this:

diff --git a/test-file.txt b/test-file.txt
index 19e6adf..eb6bb81 100644
--- a/test-file.txt
+++ b/test-file.txt
@@ -1 +1 @@
this is a [-short-]{+slightly longer+} line

Or, with colorization, instead of this:

result of just <code>git diff</code>

You might get this:

result of <code>git diff --word-diff</code>

Now, if you're comparing a really long line, you may still have issues with the pager situation you originally described, and which has been addressed, apparently to satisfaction, in other answers. Hopefully this gives you a new tool, though, to more easily identify what on the line has changed.


To use less as the pager and make line wrapping permanent you can simply enable the fold-long-lines option:

git config --global core.pager 'less -+S'

This way you do not have to type it while using less.

Cheers