Problem: Let’s say we are a team of developers who are collaborating using Git repository. One day a developer makes a few line of changes & commits the code file. Other team members take a pull & use diff to compare the changes with last Git revision. They find out that the whole file has changed. On taking a deeper look, the team figures out that all lines have whitespace added. There is no actual difference except the few lines when whitespace is ignored.
So why this happens? The issue most likely happens when developers use different operating systems in their local machines. Some might be using Linux or Mac. Others might be in Windows. The problem is EOL (end of line) character is different in different operating systems.
- Unix and Macintosh (Mac OSX): Line ends with LF character which is represented by \n.
- Older Macintosh: Line ends with CR character which is represented by \r.
- Windows: Line ends with a combination of CR and LF characters represented by \r\n.
Suppose most of the developers in the team are using new Mac or Linux to do the programming. So files will end with LF character \n. These files get checked into Git repository. A new developer in the team starts using Windows laptop with Eclipse IDE. He checks out the codebase. He then opens a file, modifies few lines of code in Eclipse & saves it. Eclipse would modify the file based on Windows EOL character which is \r\n. So all the EOL characters get changed to \r\n (CR followed by LF character) automatically which is invisible to the developer. Developer might not notice it while checking in the code in Git. May be the diff file feature in Eclipse has whitespace ignore option enabled. Once the code is committed other team members will see whole file changed when they do a difference with the previous Git revision.
Solution: As you can see if team members are using Linux based operating system or using new Mac, they won’t be having this trouble. End of line character is same in these platforms. And generally these are the common operating systems preferred by developers. The problem occurs when someone starts using Windows. There are couple of ways to solve this.
- In Eclipse IDE, set core.autocrlf settings to true. This autocrlf is Git specific settings. It will make sure that EOL character is changed to Unix format when the file gets committed to Git. We need to go to Window > Preferences > Team > Git > Configuration & set the value there.
- In Eclipse IDE, set line delimiter character to Unix style. We can do it from Window > Preferences > General > Workspace section.
Any one of the above two solutions should work & fix the problem.