131

I'm selectively committing parts of a large file and I'd like to see more context around each hunk. Is this possible?

Shawn J. Goff
  • 4,635
  • 8
  • 34
  • 38

4 Answers4

178

Short answer: no.

git diff has the -U<n> option which allows you to customize the number of lines to show around a change. For example, git diff -U5 ... will show 5 lines of context. As far as I can tell, there is no such option available for the diff display in the interactive mode.

hammar
  • 138,522
  • 17
  • 304
  • 385
8

This is possible using the GIT_DIFF_OPTS environment variable.

For example:

GIT_DIFF_OPTS=-u10 git add -p

will display 10 lines of context.

See https://git-scm.com/book/en/v2/Git-Internals-Environment-Variables#_diffing_and_merging .

pavels
  • 81
  • 1
  • 1
4

To confirm, this is still not possible in 2019.

An external tool like jjlee/git-meld-index can help:

git-meld-index runs meld -- or any other git difftool (kdiff3, diffuse, etc.) -- to allow you to interactively stage changes to the git index (also known as the git staging area).

This is similar to the functionality of git add -p, and git add --interactive.

In some cases meld is easier / quicker to use than git add -p or the staging feature in tools like git gui.
That's because meld allows you, for example, to:

  • see more context,
  • see intra-line diffs
  • edit by hand and see 'live' diff updates (updated after every keypress)
  • navigate to a change without saying 'n' to every change you want to skip
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
1

I just want to add that you can get the diff with full context using a solution like this https://stackoverflow.com/a/40683517/15410684 by Ezra.

git diff -U$(wc -l MYFILE)

gets you pretty close

Nate S.
  • 11
  • 2