Search code examples
gitbinaryfilesgit-add

Why doesn't git add --patch work with git add -N of a binary file?


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 

Solution

  • 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