Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should git change the ending of line? (control+M or ^M at the end of the first line)

I use git to pull my code from a windows machine.

When the other developers pull the files from the server on their liunx machine they have the ^M at the end of the first line.

How can make git to take care of this for me?

I mean, git should change the ending of line (delete control+M or ^M at the end of the first line) when I push (from my window machine) the code on the server.

like image 848
antonjs Avatar asked Feb 10 '12 09:02

antonjs


People also ask

Does git change line endings?

Git has changed line endings to match your new configuration. To ensure that all the line endings in your repository match your new configuration, backup your files with Git, delete all files in your repository (except the . git directory), then restore the files all at once.

How do I fix git line endings?

#Set LF as your line ending default. #Save your current files in Git, so that none of your work is lost. #Remove the index and force Git to rescan the working directory. #Rewrite the Git index to pick up all the new line endings.

What does M mean in git status?

^M represents carriage return. This diff means something removed a Unicode BOM from the beginning of the line and added a CR at the end.

Should I use CRLF or LF?

Whereas Windows follows the original convention of a carriage return plus a line feed ( CRLF ) for line endings, operating systems like Linux and Mac use only the line feed ( LF ) character. The history of these two control characters dates back to the era of the typewriter.


1 Answers

This GitHub help page deals with this very specific topic and drives one through the steps to correctly configure your Git configuration.

Basically, if you're working on Mac/Linux, use

$ git config --global core.autocrlf input

If you're rather a Windows guy, use

$ git config --global core.autocrlf true

Note: This will convert your line endings on the fly, while performing a checkout or a commit and ensure that your text files will have LF line endings in your repository while having native line endings in your working directory.

Note 2: This will not rewrite the history of your repository. Existing commits in the repo will keep their potentially mixed lines endings.

Note 3: Ensure that every committer executes this configuration step before their next commit.

An alternative approach, which doesn't involve compelling everyone to change their config, exists. It requires adding a specific file name .gitattributes to your repository. More information about his topic in the git official gitattributes documentation.

Note 4: Tim Clem, a githubber, published a very detailed blog post (Mind the End of Your Line) about line endings, related configuration entries and gitattributes benefits. It's an absolute must read if you're willing to get a good grasp of the concepts, the "why" and the internal machinery.

like image 198
nulltoken Avatar answered Nov 15 '22 20:11

nulltoken