git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: Merge conflicts in .gitattributes can cause trouble

From
Duy Nguyen <pclouds@gmail.com>
Date
Oct 4, 2016, 11:26 UTC
Message-ID
<CACsJy8CSHepObhcSYHOYgY0oev6wEeNgpy+x1_c_QhLtLA8Xzg@mail.gmail.com>
In-Reply-To
<248A6E81-8D5C-4183-9756-51A0D5193E3E@gmail.com>
On Tue, Oct 4, 2016 at 5:19 PM, Lars Schneider <larsxschneider@gmail.com> wrote:
Show 44 quoted lines
> Hi,
>
>
> If there is a conflict in the .gitattributes during a merge then it looks
> like as if the attributes are not applied (which kind of makes sense as Git
> would not know what to do). As a result Git can treat e.g. binary files
> as text and they can end up with changed line endings in the working tree.
> After resolving the conflict in .gitattributes all files would be marked
> as binary, again, and the user can easily commit the wrongly changed line
> endings.
>
> Consider this script on Windows:
>
> $ git init .
> $ touch first.commit
> $ git add .
> $ git commit -m "first commit"
>
> $ git checkout -b branch
> $ printf "*.bin binary\n" >> .gitattributes
> $ git add .
> $ git commit -m "tracking *.bin files"
>
> $ git checkout master
> $ printf "binary\ndata\n" > file.dat # <-- Unix line ending!
> $ printf "*.dat binary\n" >> .gitattributes # <-- Tell Git to keep Unix line ending!
> $ git add .
> $ git commit -m "tracking *.dat files"
> $ git cat-file -p :file.dat | od -c
> 0000000   b   i   n   a   r   y  \n   d   a   t   a  \n
>                                 ^^^^                ^^^^  <-- Correct!
> $ git checkout branch
> $ git merge master # <-- Causes merge conflict!
> $ printf "*.bin binary\n*.dat binary\n" > .gitattributes # <-- Fix merge conflict!
> $ git add .
> $ git commit -m "merged"
> $ git cat-file -p :file.dat | od -c
> 0000000   b   i   n   a   r   y  \r  \n   d   a   t   a  \r  \n
>                                 ^^^^^^^^                ^^^^^^^^  <-- Wrong!
>
> Possible solutions:
>
> 1. We could print an appropriate warning if we detect a merge conflict
>    in .gitattributes

This is good regardless, to encourage people to resolve conflicts in .gitattributes first. A good place for this warning may be "git status"?

Show 6 quoted lines
> 2. We could disable all line ending conversions in case of a merge conflict
>    (I am not exactly sure about all the implications, though)
>
> 3. We could salvage what we could of the .gitattributes file,
>    perhaps by using the version from HEAD (or more likely, the ours stage of
>    the index) -- suggested by Peff on the related GitHub issue mentioned below

We already have code to fall back to index version in some cases, adding "fall back on merge conflicts" (and probably updating the index lookup code too because it looks for stage 0 now) sounds reasonable (especially with the warning in #1).

BTW whoever fixes this probably should do the same for .gitignore files.
-- 
Duy
Previous: Lars SchneiderNext: Johannes Schindelin
Message 2 of 6 in “Merge conflicts in .gitattributes can cause trouble”
  1. Lars SchneiderOct 4, 2016
  2. Duy NguyenOct 4, 2016
  3. Johannes SchindelinOct 17, 2016
  4. Junio C HamanoOct 17, 2016
  5. Johannes SchindelinOct 18, 2016
  6. Junio C HamanoOct 18, 2016

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.