How to see more context when `git add --patch`?
Apr 19, 12:39pm

When using git add --patch (or git add -p), Git shows you a hunk (a chunk of changes) and prompts you for action. By default, Git displays a limited amount of context around each hunk, which can make it hard to understand the full picture.

How to See More Context

1. Increase the Context Lines

Use the -U (or --unified) option to specify the number of unified context lines Git should show around each change. The default is usually 3 lines. To see more, use:

sh
git add -p -U10  # Shows 10 lines of context around each hunk

This applies to the current add -p session.


2. Set a Default for All Diffs

If you always want more context, configure Git globally or locally:

sh
git config --global diff.unified 10  # Sets 10 lines of context for all diffs

or for a specific repo:

sh
git config diff.unified 10

This affects git diff, git add -p, and other commands that show diffs.


3. Use a Pager for Better Navigation

If the diff is long, use a pager like less for easier navigation:

sh
GIT_PAGER=less git add -p

or set it in your Git config:

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

4. Edit the Hunk Manually

If you want to see the full file while editing a hunk, you can:

  • Open the file in another terminal/editor.
  • Use git diff in another window to see the full diff.

5. Use a GUI Tool

If the command line is too limiting, consider using a GUI tool like:

  • git add -p in Magit (Emacs)
  • GitKraken
  • VS Code GitLens
  • Sourcetree

These tools often provide a more visual and interactive way to stage hunks with full context.


Tip: If you want to see the full file for a specific hunk, you can also use git difftool with your preferred diff tool (e.g., vimdiff, meld, kdiff3).

Would you like help setting up a specific diff tool or pager?

Apr 19, 12:39pm

This is a shared conversation and may include unverified content that doesn't reflect Mistral AI's views. Login to resume chatting.