2

I have a git repository which is hosted in bit bucket. I use source tree to work with git. I have a pull request in which some files appear as modified, but the contents of the file are essentially unchanged. I noticed that when I compared the two branches in bit bucket and source tree. This is what the diff looks like in those tools :

-j1
-j2
-j3
+j1
+j2
+j3

This is very confusing because (1) it makes it appear as though there are many changes, even though there are no real changes, and (2) any changes to a particular line could be missed if you have a huge file (unlike the small one I showed above.)

But, when I compared the two branches in command line (git diff branch1 branch2), I saw that there is a ^M character at the end of each line in one branch which is responsible for the difference.

How do I ensure that end of line characters like ^M, TAB, SPACE etc. get removed in git so that we can avoid confusion in pull requests ? Also, how do I find out why these EOL characters got added in the first place ?

Erran Morad
  • 4,563
  • 10
  • 43
  • 72
  • Use better, smarter tools to compare (Beyond Compare, etc.), also use .gitattributes – ddbug Oct 18 '19 at 01:41
  • @ddbug - ok. but, I need to prevent this problem in the first place because it will show up in the git host. I have no control over that host. So, I need a better fix. – Erran Morad Oct 18 '19 at 01:43
  • 1
    Then use a smarter editor which saves files in your preferred way, defined via editorconfig for example. – ddbug Oct 18 '19 at 01:49
  • `^M` seems to be the carriage return character, so it seems it may have to do with line endings. You might want to see [this post](https://stackoverflow.com/questions/10418975/how-to-change-line-ending-settings) to see if it may help. – Leftist Tachyon Oct 18 '19 at 02:19
  • 1
    @Borat - I have seen this issue while using both windows and Linux to change code. I had the clone of a repo created in windows machine and also in a VM based on redhat Linux sometime back. [This](https://stackoverflow.com/a/1889699/3981539) helped me get it resolved. – Kavitha Karunakaran Oct 18 '19 at 02:24
  • 1
    Do not try to use config for this.... this is the old way to work around this problem. Try .gitattributes file instead. – eftshift0 Oct 18 '19 at 03:08

1 Answers1

1

Try first

git config --global core.autocrlf false

Then clone again, and make sure there is no diff.
That will ensure there is no "automagic" eol conversion.

Then you can add in a .gitattributes eol directives (example here)

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250