Getting your Symfony2 Team On Board With Git Line Endings!

Lately I ran into this help page from github here.
The main problem that this solves and the one I ran into was that I made some modifications to a file in a git repository and to my surprise I was met by an ugly scenario when doing `git diff` showing strange characters `^M` which were showing red and ugly:

I tried in vain to get rid of these windows format line endings. Then was redirected to the link above but just initially set my .gitconfig to no effect.
In order to solve this there were two options: (1) waiting until project manager would make a transition from an unclean repo to a reformatted one, or (2) just cope with it and either run a nice utility called `dos2unix` (you can install it by `sudo apt-get install dos2unix on ubuntu) to filter those characters, stash my changes, then commit the whole file changes after the dos2unix command, yes this command changes all the file, then apply my stashed changes, then commit again. The latter solution is not very nice as there is an ugly bump in history as to who change what in the file. So I just went ahead and made my changes and accept for one only time to see those weird characters on my `git diff`. The reason why those characters were there was that for that particular repository on some version at some point the file got edited in Microsoft products by someone using Windows OS.

The morale of the story is that in order to avoid these problems with strange end of line characters we could just make our project totally independent of what the OS of the team’s developers use. The `.gitattributes` file accomplishes this by telling git what to do or how to behave when checking out these files. Following is a `.gitattributes` I wrote in order to accomplish this for a symfony2 generic project. It is my hope that people would contribute to it and would improve it. Here is the to fork and PR and following is a excerpt:

# Set default behaviour, in case users don't have core.autocrlf set.
* text=auto
# Explicitly declare text files we want to always be normalized and converted 
# to native line endings on checkout.
*.php text
*.txt text
*.ini text
*.js text
*.css text
*.cache text
*.meta text
*.phar text
app/console text
console text
vendors text
.gitignore text
// ...
# Denote all files that are truly binary and should not be modified.
*.png binary
*.gif binary
*.jpg binary
*.db binary
*.jar binary
*.ico binary
.gitkeep binary

It has inline comments that explain what each section does. Please from now on make sure you include this file in your symfony2 project.
If you liked this and want to support me going to symfonyLive in San Francisco please donate.

Leave a Reply

Your email address will not be published. Required fields are marked *