Search This Blog

Friday, July 4, 2014

Solve Line encoding(unix/windows) problems in Git branches through Eclipse

There are some possibilities when people work on the same Git hub project in unix and windows. During this time most of us face the problem with Line encoding, Even though there are no changes to the branch, we will observe the entire file has been changed. This is so cumbersome to look at each and individual file and check what actual content is modified.

There are 2 fixes fir this;
  • Fix the IDE settings for file encoding and line delimiters
  • Configure Git to automatically do the conversion for you

Git Config

Git resolves the line endings using a property called 'core.autocrlf'. The concept of autocrlf is to handle line endings conversions transparently. And it does!

Bad news: value needs to be configured manually.
Good news: it should only be done ONE time per git installation (per project setting is also possible).

How autocrlf works:

core.autocrlf=true:    core.autocrlf=input:      core.autocrlf=false:

       repo                     repo                    repo
    /        \               /        \              /        \
crlf->lf    lf->crlf     crlf->lf       \          /            \      
 /              \        /                \      /                \
Reminder: crlf = win-style end-of-line marker, lf = unix-style. Note that cr (mac-style) in not affected for any of three options above.

Yet another way to show how autocrlf works
1) true:             x -> LF -> CRLF
2) input:            x -> LF -> LF
3) false:            x -> x -> x
where x is either CRLF (windows-style) or LF (unix-style) and arrows stand for

file to commit -> repository -> checked out file

How to fix

Default value for core.autocrlf is selected during git installation and stored in system-wide gitconfig (%ProgramFiles(x86)%\git\etc\gitconfig).

Moral (for Windows):
  • use core.autocrlf = true if you plan to use this project under Unix as well (and unwilling to configure your editor/IDE to use unix line endings)
  • use core.autocrlf = false if you plan to use this project under Windows only
  • never use core.autocrlf = input unless you have a good reason to (eg if you're using unix utilities under windows or if you run into makefiles issues)

Eclipse IDE Settings

Project level Settings
  • Once you clone the project in eclipse and link it it got repository view,
  • Go to Git perspective
  • Right click on the corresponding git project and select properties.
  • There update your core.autocrlf to true
Workspace Settings
  • Go to Windows-> Preferences-> Workspace
  • Set Text file encoding to other->UTF-8
  • Set New text file line delimiter to other->Unix
  • Save the settings and apply
  • If still problem persists, then select the required folder and click on File->Convert Line Delimiters -> Unix
These 2 steps will solve the problem.

No comments:

Post a Comment