168

How do I create a patch with full context?

I tried --unified=2000, which gives 2000 lines of context:

git diff --unified=2000 branch master --no-prefix > patch

How do I include all the lines in the file without having to specify the maximum number of lines?

1

7 Answers 7

140

This seems to work pretty nicely:

git diff --no-prefix -U1000

With the caveat:

The -U flag specifies lines of context. You might need to increase this if there are more than 1000 lines between your changes.

5
  • 36
    The -U option you suggest is the same as the --unified= option used by the asker. The only difference is that you specify fewer lines of context, 1000, than the asker used, 2000. @balki wanted to know how to increase the number to infinity, yet you suggest cutting the number in half. Why? Commented May 3, 2016 at 19:31
  • 6
    @LS: yep, I realise now, but overlooked that a couple of years ago. Still, it's a bit more apparent what's going on than in the question and seems to help the odd person who lands here.
    – c24w
    Commented May 3, 2016 at 19:38
  • Thanks for this, it also works great with git show!
    – Shakeel
    Commented May 23, 2016 at 18:38
  • 3
    but it doesn’t answer the question at all, which is “Is there a way to tell git to include all the lines in the file for patch without having to specify maximum lines?” Commented Jan 19, 2017 at 10:13
  • 3
    The --no-prefix option gets rid of the “/a/” and “/b/” destination prefixes that show up by default. (linked page) Commented Dec 18, 2017 at 9:26
84

I know this is old, but I also dislike hard-coded solutions, so I tested this:

git diff -U$(wc -l MYFILE)

Using -U seems to be the only way to approach the issue, but using a line count promises that it will work for even a small change in a very large file.

7
  • 1
    < is not necessary. git diff -U$(wc -l MYFILE) MYFILE
    – balki
    Commented Nov 18, 2016 at 22:12
  • 3
    Thanks @balki, I tried your suggestion and noticed that <pre>$(wc -l MYFILE)</pre> expands to the line count followed by the file name, so the second use of the filename can be omitted also. I'm updating my answer to reflect this.
    – Ezra
    Commented Nov 22, 2016 at 17:36
  • 13
    It's a diff, there are two version of the file. What if the version not on the disk was twice as long? Isn't -U with a really big number actually safer?
    – Eloff
    Commented Aug 14, 2018 at 13:29
  • 1
    We could work something in from this answer to do a shell calculated max: unix.stackexchange.com/a/186703 - in my mind, a user is probably going to put this in their git config as an alias, so it is acceptable to be verbose. Ideally, there would be a --full-context diff option built into git.
    – Ezra
    Commented Aug 15, 2018 at 15:26
  • 7
    git diff -U$(wc -l MYFILE | awk '{print $1}') MYFILE is a better answer that correctly parses the output of wc by only getting the number of lines without whitespace, not relying on the unquoted output of a subshell to create two arguments, and works on macOS/BSD.
    – anishpatel
    Commented Feb 18, 2019 at 20:36
11

Note: git1.8.1rc1 announce (December 8th, 2012) includes:

A new configuration variable "diff.context" can be used to give the default number of context lines in the patch output, to override the hardcoded default of 3 lines.

so that could help, here, generate a more complete context.

3
  • 8
    Yet that doesn't have an option to say 'All lines in the file'
    – balki
    Commented Dec 11, 2012 at 16:00
  • 3
    I suspect that putting a large number, that would simulate "all the lines"
    – VonC
    Commented Dec 11, 2012 at 16:04
  • 7
    "I suspect that putting a large number, that would simulate "all the lines"" ... except when it doesn't and then things break. All is synonymous with infinite, and a very large number is just that -- a number, not infinity.
    – Trenton
    Commented Sep 25, 2015 at 19:42
7

I simply use

git diff -W Git.md

to list the full patch to a file. From man git diff:

-W, --function-context
Show whole function as context lines for each change.

1
  • That's a relevant switch to know about, however it doesn't necessarily include the whole file (most files will have more than one function).
    – David Cook
    Commented Aug 23, 2023 at 23:44
5

Got inspiration and so I added a git alias.

$ cat ~/.gitconfig | fgrep diff
        df = "!git diff -U$(wc -l \"$1\" | cut -d ' ' -f 1) \"$1\""
$ git df <file>

Update:

Just found git df does not work sometimes, due to directory change when executing git alias. (See git aliases operate in the wrong directory). Also @Moises Soto mentioned on macOS the wc command works differently (6 spaces at the front of the wc -l output) and suggested to use awk. So this is the updated version:

$ cat ~/.gitconfig | fgrep df
        df = "! [ \"$GIT_PREFIX\" != \"\" ] && cd \"$GIT_PREFIX\"; ~/bin/git_df.sh"
$ 
$ cat ~/bin/git_df.sh
#!/bin/bash
for FILE in $@; do
    git diff -U$(wc -l "${FILE}" | awk '{print $1}') "${FILE}"
done
exit 0
2
  • For a single line alias: df = "!cd -- ${GIT_PREFIX:-.}; git diff -U$(wc -l \"$1\" | cut -d ' ' -f 1) \"$1\""
    – fizzyh2o
    Commented Sep 15, 2020 at 6:46
  • @fizzyh2o yes your version also works if git df only 1 file, however my version supports git df multiple files.
    – Yun Wu
    Commented Feb 24, 2022 at 22:56
2

This worked for me on macOS:

git diff -U$(wc -l main.htm | xargs)

see "How to trim whitespace from a Bash variable?"

-1

Previously accepted solutions don't work for me when viewing a specific file/commit (the -U option seems to mess with rev/path parsing), but --inter-hunk-context= works in this case on git version 2.24.0:

git diff \
    --no-prefix \
    --inter-hunk-context=2000 \
    master -- \
        path/to/file.py

If you don't know the file size, you can of course find it with wc -l instead of hard-coding it:

git diff \
    --no-prefix \
    --inter-hunk-context=$(wc -l path/to/file.py) \
    master -- \
        path/to/file.py
3
  • For me, this only shows three lines of context before the first change and three lines of context after the last change.
    – ma11hew28
    Commented Apr 12, 2021 at 13:08
  • What version of git were you using? Did you replace "path/to/file.py" with your own file path? Just tested (the first variation with 2000 hardcoded) again on git 2.12.1 on windows and it works just fine.
    – stefco
    Commented May 1, 2021 at 23:49
  • Version 2.24.3. Yes, I did. But sorry, let me clarify my last comment: Your solution also (of course) shows all the lines between (and including) the changed lines, but I think @Machavity wants to "include all the lines in the file". For example, try your solution on a file with at least five lines and with all its changes after the first four lines. If what that shows you includes the first line of the file, then maybe you have the diff.context variable in your Git configuration file set to a number larger than the default of 3.
    – ma11hew28
    Commented May 3, 2021 at 22:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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