Skip to content
Advertisement

Git whitespace/line ending

I’m going to give you a simplified version of what happen and where I am right now. I have tried different solution but I’m not even sure where does my problem come from.

I made some modification on my branch B which is a fork of the branch A of an upstream repository. Then I tried to Pull Request to merge A and B.

When creating this PR there was some conflict so I tried to resolve them. However, by trying to do so I realized thanks to git diff that some of the file showed as all lines deleted and re-added: @@ -977,16 +977,16 @@ p#splash-text span {

To have a minimal example I aborted the merge then pull into a new branch C from upstream A.

Now that I’m on C if I open the file I had problem and just save it without modifying I get the following with git diff:

@@ -977,16 +977,16 @@ p#splash-text span {

 .other div#carshare .box.active {
     background-color: green;
-}
-
-.other div#bluephone .box.active {
-    background-color: #2880ca;
-}
-
-.box_col {
-    vertical-align: middle;
-}
-
+}^M
+^M
+.other div#bluephone .box.active {^M
+    background-color: #2880ca;^M
+}^M
+^M
+.box_col {^M
+    vertical-align: middle;^M
+}^M
+^M
 .leaflet-control-locate a {
        padding: 3px 0px 0px 0px;
 }

I read a lot about line endings and found out that ‘^M’ is the line ending placed by windows but the other contributor work under Linux so I’m a bit lost on why the recent modification he made have ‘^M’ on my local.

Since I’m on Linux I tried the following: git config --global core.autocrlf input I also tried to set it to true but it didn’t resolve my problem.

The other contributor had nothing set for autocrlf, so that might be the problem?

Now if I do a small modification but run git diff --ignore-space-at-eol I get the following:

@@ -697,7 +697,7 @@ div#otp-planner-optionsWidget-scollPanel {
    padding:10px;
     }

    -.otp-layerView-inner fieldset { border-top:1px solid #000; }
    +.otp-layerView-inner fieldset { border-top:1px solid gray; }^M
     .otp-layerView-inner fieldset legend { text-align:center; }

So I would like to commit and push then PR this change but I don’t want any of the line ending to follow as well.

Is there a way to commit just this single change and not all the change introduce by the line endings?

Or how can I not have all this change?

I read the man page of git config and read other question about white space but the solutions given don’t seem to work or apply for my case.

If you have idea suggestion on how to resolve this problem and not having it in the future let me know.

Advertisement

Answer

The .gitattributes file can help with this situation. To maintain consistent unix line endings use:

* text eol=lf

I would recommend actually checking the .gitattributes file into your repo so everyone has consistent line-endings.

Next, run the dos2unix utility on the files that you altered to strip out the windows line-endings:

dos2unix file.txt

Finally, interactively rebase your change onto the branch you are trying to contribute to, and force push to update your pull-request:

git fetch
git rebase -i origin/A
git push -f origin C
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement