1

git add --patch doesn't work with binary files added with git add -N. Does anyone know why? In the following example you can see that git add --patch picks up the text file a but not the binary file 0.

echo a > a
echo '\0' > 0
git add -N 0
git add -N a

git add --patch
diff --git a/a b/a
index e69de29..7898192 100644
--- a/a
+++ b/a
@@ -0,0 +1 @@
+a
Stage this hunk [y,n,q,a,d,/,e,?]? y 
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Tom Ellis
  • 9,224
  • 1
  • 29
  • 54

1 Answers1

3

The implementation explicitly excludes binary files. See line 7:

 1  sub patch_update_cmd {
 2    my @all_mods = list_modified($patch_mode_flavour{FILTER});
 3    error_msg sprintf(__("ignoring unmerged: %s\n"), $_->{VALUE})
 4      for grep { $_->{UNMERGED} } @all_mods;
 5    @all_mods = grep { !$_->{UNMERGED} } @all_mods;
 6
 7    my @mods = grep { !($_->{BINARY}) } @all_mods;
 8    my @them;
 9
10    if (!@mods) {
11      if (@all_mods) {
12        print STDERR __("Only binary files changed.\n");
13      } else {
14        print STDERR __("No changes.\n");
15      }
16      return 0;
17    }
18    if ($patch_mode_only) {
19      @them = @mods;
20    }
21    else {
22      @them = list_and_choose({ PROMPT => __('Patch update'),
23              HEADER => $status_head, },
24            @mods);
25    }
26    for (@them) {
27      return 0 if patch_update_file($_->{VALUE});
28    }
29  }

git-add--interactive.perl#L1310-L1338

jsageryd
  • 4,234
  • 21
  • 34
  • Interesting. Any idea why? – Tom Ellis Apr 02 '18 at 10:19
  • 2
    It has been like this since the introduction of the command in 2006 (commit 5cde71d64aff). I can only speculate why -- perhaps it is because it does not make sense to patch-add a binary file, since it cannot be split into hunks. – jsageryd Apr 02 '18 at 10:22