2

I maintain a private Git repository with all of my config and dotfiles (.bashrc, profile.ps1, .emacs etc.).

On Windows this repository is stored under C:\git\config. Most applications expect the files to be elsewhere, so I added hard links between the repository and the expected locations.

Example

On Linux .emacs is located in ~/git/config/.emacs but emacs expects it to be at ~/.emacs. I run:

$ sudo ln -s ~/git/config/.emacs ~/.emacs

On Windows my .emacs is located in C:\git\config\.emacs, but emacs expects it to be in C:\users\ayrton\.emacs. I run:

PS> cmd /c mklink /H C:\users\ayrton\.emacs C:\git\config\.emacs

Issue

On Linux this seems to work fine: when I update the original file, the contents of the link update and everything stays in sync.

On Windows, the links break after a period of time and the files become out of sync (the file contents are different).

Why do the links break on Windows? Is there an alternative solution?


I've seen this StackOverflow post: Can't Hard Link the gitconfig File

So I’ve finally found a solution that takes the best of both: put the repo in a subdirectory, and instead of symlinks, add a configuration option for “core.worktree” to be your home directory. Now when you’re in your home directory you’re not in a git repo (so the first problem is gone), and you don’t need to deal with fragile symlinks as in the second case. You still have the minor hassle of excluding paths that you don’t want versioned (eg, the “*” in “.git/info/exclude” trick), but that’s not new.

The problem here is that the expected locations are different on Windows vs. Linux. For example, VSCode expects the user settings to be in:

  • Linux: $HOME/.config/Code/User/settings.json
  • Windows: %APPDATA%\Code\User\settings.json

Ideally I would like my repository to be platform independent. If take the core.worktree approach (e.g. make core.worktree be / or C:\, then exclude everything except specific files) I would have to maintain two copies of some configuration files when their absolute paths differ across operating systems.

Community
  • 1
  • 1
Ayrton Massey
  • 471
  • 3
  • 13

1 Answers1

1

Hardlinks can break if a editor opens/creates the file as a new blank file each time you save. It would not surprise me if Notepad did this because it reads the entire file into memory and has no need for the original file after it has loaded the file.

You can try to create a file symlink instead of hardlink on Windows.

Anders
  • 97,548
  • 12
  • 110
  • 164
  • I think I tried using the regular link command on Windows (`cmd /c mklink C:\users\ayrton\.emacs C:\git\config\.emacs`) but some programs (e.g. emacs) would complain that they could not read the files. – Ayrton Massey Apr 25 '17 at 10:06