Search code examples
eclipsegitterminalline-endings

Line endings cohabiting Terminal and Eclipse Git


We are corking on a project and managing our code versions with Git. We have one branch per feature, one branch per version we are maintaining... The problem is, we connected the project to git using command line (Eclipse doesn't provide enough features to do that correctly) but generally we use Eclipse to commit, push... In addition sometimes Eclipse didn't want to let us push, and we were forced to push in command line.

This situation resulted with some files on remote being on crlf line endings and some other with lf line endings. All that caused by Eclipse and terminal acting differently with libne endings, and I realised that too late. I am now searching for a simple, proper way to make all files auto crlf at true and a procedure that guaranty the same behavior on Terminal and Eclipse on all git clients (basing behavior on terminal).

(sorry for bad English and thanks by advance ) !

--EDIT--

I should have precised I am searching for a solution that would work for converting existing files too, converting remote files into lf on remote and crlf on the repositories


Solution

  • You can try adding and committing a .gitattributes file in the repository with this entry.

    * text eol=crlf
    

    (Reference: https://help.github.com/articles/dealing-with-line-endings/)

    Git will always convert line endings to CRLF on checkout. You should use this for files that must keep CRLF endings, even on OSX or Linux.

    Illustration:

    Consider there are 2 files in existing repo (say test-repo) with one line in each.

    file_a.txt
    file_b.txt
    

    Verify file endings as \n.

    test-repo (master)$ od -c file_a.txt
    0000000    T   h   i   s       i   s       f   i   l   e   _   a  \n
    0000017
    
    test-repo (master)$ od -c file_b.txt
    0000000    T   h   i   s       i   s       f   i   l   e   _   b  \n
    0000017
    

    Add a .gitattributes file in repository.

    test-repo (master)$ vim .gitattributes
    

    Add entry.

    * text eol=crlf
    

    Commit and push .gitattributes.

    test-repo (master)$ git add -A
    warning: LF will be replaced by CRLF in .gitattributes.
    The file will have its original line endings in your working directory.
    
    test-repo (master)$ git commit -m "Adding .gitattributes file"
    
    test-repo (master)$ git push
    

    Add a new file, and type a line of text in it.

    test-repo (master)$ vim file_c.txt
    

    Verify file ending as \n.

    test-repo (master)$ od -c file_c.txt
    0000000    T   h   i   s       i   s       f   i   l   e   _   c  \n
    0000017
    

    Commit and push the added file.

    test-repo (master)$ git add file_c.txt
    warning: LF will be replaced by CRLF in file_c.txt.
    The file will have its original line endings in your working directory.
    
    test-repo (master)$ git commit -m "Added file_c.txt"
    
    test-repo (master)$ git push
    

    Clone the repository to another directory on disk (say with the name temp_dir). Note the endings are now \r \n in lieu of \n.

    temp_dir/test-repo (master) $ od -c file_a.txt
    0000000    T   h   i   s       i   s       f   i   l   e   _   a  \r  \n
    0000020
    
    temp_dir/test-repo (master) $ od -c file_b.txt
    0000000    T   h   i   s       i   s       f   i   l   e   _   b  \r  \n
    0000020
    
    temp_dir/test-repo (master) $ od -c file_c.txt
    0000000    T   h   i   s       i   s       f   i   l   e   _   c  \r  \n
    0000020