16

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.

antonjs
  • 14,060
  • 14
  • 65
  • 91

3 Answers3

25

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.

snowe
  • 1,312
  • 1
  • 20
  • 41
nulltoken
  • 64,429
  • 20
  • 138
  • 130
  • 1
    Nice job referencing the GitHub help page (I like the way they present some material). To be even more _meta_, GitHub references [Charles Bailey's answer on StackOverflow](http://stackoverflow.com/a/1511273/320399). – blong Sep 30 '15 at 14:44
1

Try setting the core.autocrlf to true (see git-config).

Peter Svensson
  • 6,105
  • 1
  • 31
  • 31
0

This isn't a problem with GIT it's a problem with how the file was originally created/uploaded and stored. The best solution is finding an editor that allows you to set the end of line type to unix's.

This seems to have a way to do it on the windows side with GIT, but I think that's a bad way to do it in general

http://lostechies.com/keithdahlby/2011/04/06/windows-git-tip-hide-carriage-return-in-diff/

thenetimp
  • 9,487
  • 5
  • 29
  • 42