3

I just installed ubuntu along side windows 7. All of my git local working folders are on a separate data partition.

Everything is committed in windows 7's git, but in ubuntu's git, running git status shows everything as modified. When I tried git log all the history is still there.

I don't want to commit everything every time I switch to the other OS to work. Is there a solution?

Heisenberg
  • 8,386
  • 12
  • 53
  • 102

3 Answers3

4

Your problem is that when you check out files on Windows with the git default config, they are created with CRLF (the windows default) line endings in your working directory, but committed as LF for cross-platform compatibility.

Now your Linux sees the CRLF on every line and says that it’s different to the LF in the repo. That’s why every line is reported as different.

I would suggest setting the line endings to LF on windows. In a previous answer I explained the details of how to do that. Following those steps will also enable line-ending normalization to LF on linux, which will avoid problems if you accidentally create some CRLF on windows and commit that in linux later on.

You can also just disable line ending normalization completely, but that is likely to cause trouble in the future, unless you only use a completely fixed set of editors, whose line ending handling you know very will.

Community
  • 1
  • 1
Chronial
  • 66,706
  • 14
  • 93
  • 99
2

The problem is that the newline convention on Windows differs from Linux, and so on the Ubuntu side every file looks like it's been modified in its entirety (with the addition of a CR right before the end of each line). This usually happens because the repo is checked out with some level of autocrlf: in the repo, the files have the LF convention, but when checked out on Windows, the files have CRLF line endings.

If you want to safely share your working copy with Windows and Linux, you should just turn off core.autocrlf.

Community
  • 1
  • 1
nneonneo
  • 171,345
  • 36
  • 312
  • 383
  • Note that disabling line ending normalization will cause problems if your editors don’t play nice. – Chronial Aug 06 '13 at 11:28
  • @Chronial: Right. Happily, most Windows editors are pretty tolerant of `LF` nowadays (notable exceptions include Notepad and, to a lesser extent, Wordpad, but nobody in their right mind should edit code with either). On the Linux side, a lot of stuff fails with CRLF endings (like shell scripts), so you pretty much have to use LF. – nneonneo Aug 06 '13 at 11:31
  • Yes, Windows editors are *tolerant* towards `LF`, but most of them will create files with `CRLF` by default. Since line ending normalization is off, they will be commited to the repo with `CRLF`. And now you just have a bigger mess than before :). – Chronial Aug 06 '13 at 11:46
  • @Chronial: Most editors I've used allow you to set the LF/CRLF-ness by default. I like making that change, because it means I get consistent behaviour (whereas with an autocrlf setting, the files only become LF after a commit). – nneonneo Aug 06 '13 at 11:47
  • If I set `core.autocrlf` to true in Windows, doesn't that mean anything that gets committed used `LF` only? By doing so, don't I ensure that all of my repos use `LF`? Does that mean problem solved? – Heisenberg Aug 06 '13 at 11:52
  • Only when stuff is committed. If you leave something uncommitted and switch to Linux, you'll still have a problem. Best make your editor save LF. (Or you can do both: have eolstyle=lf *and* setup your editor, so you have safety. But, be aware that there will always be corner cases, and so if things fail mysteriously you will know why.) – nneonneo Aug 06 '13 at 11:54
  • @nneonneo Yes, you can set most editors to play nice, but you need to take care of that, and I just wanted to note that ;). @Anh No `autocrlf` will change `CRLF` to `LF` on checkin, but also change `LF` to `CRLF` on checkout. so `autocrlf` actually *forces* problems on your setup. – Chronial Aug 06 '13 at 12:09
  • I see. This is more annoying than I thought. – Heisenberg Aug 06 '13 at 12:15
  • Your solution works! But I'm still wondering whether `autocrlf=input` on both Windows and Ubuntu will solve the problem. Doesn't that force `lf` end of line on both OS? That sounds simpler to me. – Heisenberg Aug 06 '13 at 13:01
  • Setting that to `input` might work, but note that the whole `autocrlf` mechanism is outdated and you should use attributes for that stuff, as explained in the answer I linked to. – Chronial Aug 06 '13 at 14:39
  • Yes, your solution does have the benefit of consistency if someone else on a different machine clones my repo. – Heisenberg Aug 06 '13 at 14:47
0

I'm guessing there's an issue with line endings. You probably have commited your code with windows line endings and when you checked out the code in ubuntu git converted those to unix style line endings.

Take a look at this page Dealing with line endings

crea1
  • 11,077
  • 3
  • 36
  • 46