I have a repository with a backup of various config files. I would like the repo to contain only symlinks to the real config files (which are outside of the repository), so that I don't need to manually copy the config to repo in case of any changes.

When I tried to push symlink, the file was naturally unreadable in git or on other filesystem. Is there any way to force git to always follow the link and push original file, or is there any other approach?

  • Use a hard link -- ln source destination – user96931 Nov 23 '19 at 19:52

I can think of two solutions:

Reverse the Direction of the Symbolic Link

Put the configuration file in your Git repository and make a symlink to that file as the actual configuration used.

Use a Hard Link

If the Git repository is in the same file system as the configuration location, instead of ln -s, use ln to make a hard link so that the configuration used and the Git working directory file refer to the same file.

  • Reversing the link direction is a good idea, it's the simplest solution and gets the job perfectly done. Plus, all the config files will be physically present on one place where I can have my own directory structure, which I'm a fan of. – Ondra K. Nov 24 '19 at 9:27
  • @OndraK. if this work for you, go ahead and use it! However, symlink-ing config files may break things. E.g. a BASH script that tests for the presence of a config file using [ -f /path/to/config ]. Debian's default .bashrc sourcing /etc/bash_completion would be an example. – dirdi Nov 24 '19 at 12:20

This answer is based on this great tutorial.

The trick is to use a bare repository. This way you can leave your dotfiles where they belong and at the same time keep track of them using git:

# Init repo
git init --bare ${HOME}/.config.git

# Ignore all files except the ones you want to track
config config --local status.showUntrackedFiles no

# Add an alias to your .bashrc for convenience
echo "alias config='/usr/bin/git --git-dir=${HOME}/.config.git/ --work-tree=${HOME}'" >> ${HOME}/.bashrc

# Start using your repo to track your dotfiles
config add ~/.bashrc ~/.vimrc ...

Head over to https://dotfiles.github.io/ for more info on this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.