Welcome to PhoneASK, where you can ask questions and receive answers from other members of the community.

Fixing the line-endings in a Git repository using the `.gitattributes` file

asked 一月 12 at 04:36 by Abdelilah

What needs fixing:

I have a repository containing a single .md file, which contains an essay I am writing.

I edit the file from a few different computers, one running Linux and a couple running Windows.

Looking at a git diff in Windows now where I have made some changes, I can see my essay showing as nicely separated lines of text ... all about to be removed and replaced by one long line where what were paragraphs are separated by ^Ms.

I'm aware that ^M refers to Windows' CLRF line endings.

The diff result implies that I started the file in Linux (entirely possible; I don't remember) and have since saved it in Windows and that all the line endings have been replaced.

I'd like to be able to open files across both OSs with lines displaying as they should, and have a diff result that shows line breaks (rather than ^M placeholders) and only changes to the actual content.

What I've tried:

I have done some background reading, read a nice overview of line-endings and Git settings, and even tried following the commands in another Stack Overflow question.

As it stands I have one .gitattributes file at the top-level of repository which I've committed to the repository itself. It contains just two lines:

# These files are text and should be normalised (convert Windows' CLRF to LF)
*.md text

I've tried this (source):

git rm --cached -r .
git reset --hard
git add .
git commit -m "Normalize line endings"

And this (source):

git rm --cached -r .
git config core.autocrlf input
git diff --cached --name-only -z | xargs -0 git add
git commit -m "Fixed crlf issue"

In the second case, the last command tells me there is nothing to commit. (I also don't like the idea of changing the core.autoclrf since I'm trying to do this purely through .gitattributes, but I'm getting frustrated.)

Happy to answer questions and provide more details. Any ideas where I could be going wrong? Am I missing a step?

Your answer

1 Answers

answered 一月 12 at 07:47 by Dylan Lacey

Try just using

*.md text eol=native

instead of

*.md text

in your .gitattributes.

I did set up a small test repo with a CR-LF file in it and followed your first procedure to perform normalization:

git rm --cached -r .
git reset --hard
git add .
git commit -m "Normalize line endings"

That correctly normalized file on check-in. But, weirdly, it still maintained CR-LF when checking-out, even if I am on OSX.

Supposedly, default value for core.eol is native. So, I expected git to checkout my files using just LF. But it seems that for some reason, it's just not doing that. So, my understanding of .gitattributes is flawed, or we have a bug to be filed to git...

In any case, explicitly setting eol=native in .gitattributes did the trick for me, as I said.

Related Questions

I have some Python scripts that have an $Id$ field set by .gitattributes at checkout. How can I use this ident string to check out (or otherwise identify) the specific version of the file? The problem
Background: git -- or any other SCM -- can successfully merge Storyboards, xib's, and pbxproj files as at their core they are just xml. Sometimes there are conflicts which can be resolved by the usu
I have a file that is loitering in my git status saying it has been deleted, but when I try to git rm is, I get ***/***.php is outside repository. The folder it is in is certainly within the repo, so
I am trying to get the hang of git. We have a main git repository that is our master website, we pull data from it but cannot push to it. we also have individual repositories for each developer. Now w
How would I get the line count for all the different file types in my repository? So for example, if my repository contained 3 file types: java xml (no file extension) I want the output to be someth
Is there a simple git command to determine the creation date of a file in a repository, i.e. the date it was first added? It would be best if it can determine this even through file renames/moves.
I am working on a project configured on Windows 7 machine. I am the only developer working on it, so I want to setup a private Git repository on the same machine and want to work by creating git branc
I am working on a project where we manage external libs/headers and qa with git. Here is what every developers' directory structure looks like: ~/dev/proj ~/dev/ext ~/dev/qa proj, ext and qa are diff
I apologize in advance, I feel this is a stupid question :/ I started learning GIT a while back and used it on a few projects. Soon after I backed everything up and reformatted my computer. Then I mad
I have a shared hosting package and I have installed git from source. I created a repo and intend to create a couple more, but I was wondering if there is any way to secure the git repository. Current
I use BitKeeper at work, and I would like to have a basic code backup for myself at home (considering I back up very rarely) // I have never used git before so I need lots of help I thought it might b
I tried looking for a good tutorial on reducing repo size, but found none. How do I reduce my repo size...it's about 10 MB, but the thing is Heroku only allows 50 MB and I'm no where near finished dev
One situation in Git that I still find confusing is this: $ git clone https://github.com/dude1/project Whoops, that's not really the right version. I'll switch over: $ git remote add dude2 https://gi
I have a file in my git repository which sometimes receives bug fixes in another git repository. I would like to be able to pull those commits into my repo. For example: project_a/ /.git /src/somefile
I've set up my GIT repository on my server using the following in the appropriate folder (the root of my subdomain, git.mydomain.com): mkdir foo.git cd foo.git git init --bare --shared This would mak
I run a git server which contains only the bare remotes. Say, I have one bare remote called DIG.git which I usually clone with: git clone and I want to rename the bare remote
I am using Eclipse Egit on Suse 11.0 with git version Egit: 2.1.0 When I am cloning a repository from gerrit in Egit, I see a lot of typechanges in />git status ... typechange: src/TestPor
I have exported tar.gz file from SVN. How can I create GIT repo from this?
I am planning to make a fork of an open-source project, but I want to switch to GIT. The project is using SVN, but there is no TRAC available, so I can't just download changesets without having SVN on
When I try to push my app to Heroku I get this response: fatal: 'heroku' does not appear to be a git repository fatal: Could not read from remote repository. Please make sure you have the correct acce
I have the following situation: Have 4-5 different software modules part of a bigger software project. All of them are set as separate git repos. When i commit a feature, usually it spans more than a
I need to to a git checkout in pure PHP. I already tried this ( http://www.phpclasses.org/package/5310-PHP-Retrieve-project-files-from-GIT-repositories.html ) with HTTP and SASL, but I didn't really w
So, I'm pretty new to the whole GitHub thing. Consequently my repo looks like a messy pile of shit. Anyway, I wanted to put in a little effort to clear it up, so I pulled it down, updated the folder s
I'm trying to clone a local copy of Hacker's Keyboard, througth Git, I have tried the command : $ git clone https://code.google.com/p/hackerskeyboard/ but getting fatal: remote error : repository not
I accidentally deleted a project with a .git/ in it. There is only this local repository. I recovered many files using recuva and the project is restored there. But the .git-things aren't back, becaus
How can I unpack a gem specified in bundler by a :git => url? My Gemfile has gem 'my_gem', :git => 'git@github.com:xxxxx/xxxxx.git' $ bundle correctly reports the gem as available, and my code
If I have an array and all I have is an upper limit on how big the array can get, and if the number of elements that get added can be considerably smaller than this limit, is it always the right choic
Is it possible to host a Maven repository that serves the artifacts (at least the jar files) from SourceForge's file release system (since they are already there and SF provides download statistics an
I am using Bit bucket as my hosting site for the distributed version control systems (DVCS) Git. While Cloning the project and bringing into my eclipse it is giving me the error: Git repository clone
my team mate and I are struggling to get our svn repository in sync with our git one. We are working on a project and we are requested to share our code on an svn repository (hosted by the assembla.co
Quick question: I've run into a little issue, where I just need some insight into what I should do with some code I have. I have one repository, in two locations. The older code is located in /origi
I'm developing a build tool which can import dependencies from various other locations. I want to support importing svn, git, and mercurial repositories by specifying the URL for the repository. Ideal
I've done the process described at this link http://toroid.org/ams/git-website-howto It works like a charm, but now I have to revert, temporarily, the remote repository (and the checkout) to a previou
I'm getting started with Titanium Studio to build mobile apps. I'm using Beanstalkapp (or any other remote git/svn repository) for version control. I can't find a way to connect a new project to the r
When cloning from a remote repository with git clone, I can give git a hint with --reference that there is a similar local repository where some of the objects can be found, and shared in. This saves
I have a machine with a production website, I want to create a git repository in that machine in order to manage the website using git. So the first thing I did was to create an empty .git repository
Does anyone know of a Git repository browser that I can host on my own server similar to GitHub? I really like the GitHub browsing and history interface, but my favorite part is the network graph. I'd
I have a old django project needs to deploy now. I need to install the exact versions used in this project. In this project's requirements.txt there are some packages which are from GIT repos. while i
So a friend worked on a bunch of code that he never versioned, and is handing it off to me. He initially gave me version A, even though he had a more recent version B available. I put A into a git rep
I have some repos on my server with gitosis. One of them is Main, it's including all the others. I work with them from my local machine, and submodules hierarchy was built on my local machine. Gitosis
I've been trying to install Shoulda script/plugin install git://github.com/thoughtbot/shoulda.git but all I get is: removing: C:/Documents and Settings/Danny/My Documents/Projects/Ruby On Rails/_Proj
Is it possible to have a subversion repository as a submodule in git? In this project we will have several parts, but some parts will use svn and some will use git. So I am looking into a project r
I have a (Windows) GIT repository in a folder called XXX, and I have second GIT repository called YYY. I want to import the XXX repository into the YYY repository, add all XXX's change history to YYY,
We are going to use Git as our source repository. Our plan is to have a master bare repository where we will have the deliverable source code. Changes to this bare repository will be made only by a sp
I have a Git repository which contains a number of subdirectories. Now I have found that one of the subdirectories is unrelated to the other and should be detached to a separate repository. How can I
In Eclipse the import command would accept a URI but in Android Studio import only seems to accept the location of a directory on my hard drive. How should I clone my remote repository with Android St
Question What are the Git commands to do the following workflow? Scenario I cloned from a repository and did some commits of my own to my local repository. In the meantime, my colleagues made commits
-- EDIT -- I ended up copying all files to another folder an pushed from there. Without the commit history. :( Thanks for responding tho. -- /EDIT -- I'm new to git. For my html5 app I used aptana's h
I have one git repository, there are many branches many commits, I want to find the latest 10 commits, how to do this , thanks!
I'm currently open sourcing a project that we've been developing on our own personal, private git server. We want to open source part of this project on GitHub as there are still features in developme