1

I sometimes have long line of code in git repo (e.g. a json config file), and git diff will generate output like below.

long line

On an EC2 instance, I git diff doesn't wrap the content to the next line, generating following output (I can use left/right keys to navigate), which I personally prefer.

short line

Does any one know how I can config the git-diff to change from one behavior to another?

2
  • 2
    If you are using less as your pager, set the -S option. (I like to think of S as standing for Sideways Scroll.) The defaults in Git are complex: see stackoverflow.com/a/18781512/1256452. Note that setting S can interfere a bit with F: unix.stackexchange.com/q/231427/162084
    – torek
    Commented Oct 13, 2016 at 2:31
  • If the pager is vi or vim, then you can get same result of the less by doing git diff | vi - or git diff | vim - too.
    – Masashi
    Commented Dec 23, 2020 at 21:25

2 Answers 2

2

It's your terminal or pager program that wraps the lines, not git diff. Try redirecting the output of git diff into a file and open it with an editor that allows to control wrapping - you will see that the lines are not actually wrapped.

You can try this:

git diff|cut -c -$COLUMNS

Note however that it will disable colors and paging.

1
  • Passing --color to git diff will force colors again, but then the lines are visibly cut short before actually reaching '$COLUMNS', one char for each ANSI color char in the line. Commented Nov 9, 2017 at 18:10
1

Thank to torek's inline response above. Based on that, I realized I can do git diff | less -S to achieve what I want. It does better than the cut -c -$COLUMNS solution because it doesn't lose any context (I can still use left and right button to view all differences).

Not the answer you're looking for? Browse other questions tagged or ask your own question.