File synchronization between Linux and WIndows retaining similar line breaks using Git

@waterelement3

Continuing the discussion from For file synchronization between Windows and Linux, even if the files have exactly the same content, the file contents will also be sent.:

If you are using git as distributed version control system across multiple devices and operating systems, such as Windows and Linux, but you also want to keep your files in sync with Syncthing, I recommend manually configuring your git repo and making use of the official Git documentation: Git - gitattributes Documentation .

For instance, you could add a .gitattributes file to your repo with the following content: * text=auto eol=lf. This will force all your contributors to the repo to use LF line breaks, as mandated by Linux operating systems. Alternatively, you could use * text=auto eol=crlf to force Windows line breaks. Afterwards, if you have old files that you want to convert to your newest line-break rules, you can use the following commands:

git add --renormalize .
git status        # Show files that will be normalized
git commit -m "Introduce end-of-line normalization"

Furthermore, maybe it would be wise to use a workflow with autocrlf = true instead. It’s usage is very well explained here: https://stackoverflow.com/questions/1967370/git-replacing-lf-with-crlf, but that alone is not enough to prevent Syncthing to sync differing line-breaks. Though, it has the advantage of avoiding complications that may emerge when using a file with non-default line-breaks on an Operating System that wasn’t designed for it. In other words: Introducing a .gitattributes file with * text=auto eol=lf my cause issues, if you try to execute a script from your git repo, when it is run on a Windows Operating systems, as it now contains the “wrong” line breaks. I personally have not run into issues though, so this is more a hypotheses of mine, so if you can confirm or deny, please leave a message here.

I’m a big proponent of using Syncthing; I mean, obviously. However, it’s never struck me that I’d want to use it for my git repos. I highly recommend using git push/pull for git and not trying to use Syncthing to manage the files. (I know there are cases where it kinda makes sense to sync the source files, but never the .git repo.)

I am currently using it that way. There are two reasons:

  1. The whole parent directory that contains .git repo is synced via syncthing.
  2. To sync files that are not wanted to be published remotely across private devices, while at the same time having those files in the .git repo for git reasons. For example, I do have files I do not want to make public (for now), so I have a .gitignore file that prevents git detecting and pushing certain files to remote, but I still want to work on those files on my main desktop computer and on my laptop. Basically for those files, I am using Git purely as a local “version control system” and not for synchronisation across devices.

But yes, it is NOT recommended practice. I only sync with Syncthing, while making sure that the files in the .git repo are at rest, as syncing while .git is indexing files is a big no no (as folder paths on windows and linux are different) and the line-break issue is obviously a thing too.

I am actually thinking about adding the .git folder or at least parts of it to my syncthings .stglobalignore file, but then I would have to do the syncing completely via .git and as I said, I do have files I do not want to publish remotely. Hmmmm.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.