2

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?

1
  • Use a hard link -- ln source destination
    – user96931
    Commented Nov 23, 2019 at 19:52

2 Answers 2

4

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.

2
  • 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.
    Commented Nov 24, 2019 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
    Commented Nov 24, 2019 at 12:20
4

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

# Create a config alias for interacting with this repo and
# add it to your .bashrc for convenience
echo "alias config='/usr/bin/git --git-dir=${HOME}/.config.git/ --work-tree=${HOME}'" >> ${HOME}/.bashrc && source ${HOME}/.bashrc

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

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

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

1
  • 1
    Interesting solution. Thanks for that. Just FYI, in this example, you were calling on the config alias before it was set so I edited the answer and reordered the commands. Also added a source ${HOME}/.bashrc call after the alias is appended to it. I forgot how SO's answer editing works though lol. I guess when the SO gods review it it'll be saved. Dunno. Commented Apr 29, 2022 at 23:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .