3

On my mac at the root I have my .bashrc, .aliases, .zshrc..., then in another folder down the way I have a git repository of the same dotfiles. When I update the ones at the root, I update the git repo as well (manually). Is there some way to 'link' the dot files that are actually implemented at the root folder to the git repo so I only have to deal with one change?

3 Answers3

5

What about symbolic link?

Just delete the file in your root, and recreate them as

ln -s /physical/path/to/file /symbolic/link/path

For example, if you have the file /git/.aliases, ln -s /git/.aliases /root/.aliases will create a symbolic link called .aliases in the root folder. From this point on, whichever file you modify (/git or /root) will modify the other one as well.

Have a look at this link for more details on symbolic links and Mac.

Please, forgive me for the random paths, I am not a Mac users.

ThanksForAllTheFish
  • 7,101
  • 5
  • 35
  • 54
  • I'm so happy that you saved me a lot of time so that I don't have to ask a new question; pointing me towards [this link](https://gigaom.com/2011/04/27/how-to-create-and-use-symlinks-on-a-mac/), specifically [anishathalye/dotbot](https://github.com/anishathalye/dotbot#full-example) & [managing-your-dotfiles](https://www.anishathalye.com/2014/08/03/managing-your-dotfiles/) – massisenergy Sep 30 '22 at 14:17
4

I usually use symbolic links for this, eg:

mkdir ~/.dotfiles
mv ~/.zshrc ~/.dotfiles
ln -s ~/.dotfiles/.zshrc ~/.zshrc
cd ~/.dotfiles ; git init ; ...etc...

..so that the file that actually gets opened is the one under version control in your ~/.dotfiles directory.

Another option is use application-specific "include" features, eg:

  • put your "standard" configuration in ~/.dotfiles/.zshrc
  • source that file from ~/.zshrc:

    [ -e ~/.dotfiles/.zshrc ] && . ~/.dotfiles/.zshrc
    
redShadow
  • 6,687
  • 2
  • 31
  • 34
4

You can actually 'link' those files with ln. You can use either symbolic links (-s) or hard links. If you use a UNIX (like system) you should learn about links.

Symbolic links are essential special files that contain a path (relative or absolute) and are treated by the system as if they're the file on that path (this means that symbolic links can become broken).

Hard links (created by ln without the -s flag) are limited to a single filesystem and a hardlink is just a name for the same chunk of bytes as the original file. Hardlinks are indistinguishable from the original file. In fact a file is just a hardlink (=name for an inode) with count 1. (The second column in the listing of ls -l shows you the number of hardlinks (names) that exist on the filesystem for a given file.)

I've also put my dotfiles in a repo. In my repo. I have the dotfiles in a directory named payload and my repo also contains a script that links the dotfiles in payload to my home directory.

The script is as follows:

    #!/bin/bash
    shopt -s dotglob #make globbing catch dotfiles too
    #Use -f (force) to overwrite targets
    for file in payload/*; do ln -s .${PWD/$HOME/}/$file $HOME/; done #use string substitution

Edit I found that a simpler way to get the relative path in the link is to use the -r option to ln:

    for file in payload/*; do ln -s -r $file $HOME/; done 

You can also create multiple links at once by using the 3rd form of ln (see man ln):

    ln -s -r payload/* $HOME/

For a hardlink version, you could do:

 #!/bin/bash
 shopt -s dotglob #make globbing catch dotfiles too
 ln payload/* $HOME/ 

(This hardlink version won't get broken with moves and renames, but your repo must be on the same filesystem)

(All this assumes that you've only got files (not directories) in payload/ and that your links should have the same name as their targets.)

There are more sophisticated programs that do this in a more robust fashion.

Petr Skocik
  • 58,047
  • 6
  • 95
  • 142
  • 1
    I would use a hard link; if nothing else, that allows you to (re)move the git repo without removing the actual files. – chepner Sep 30 '14 at 15:06
  • @chepner: Good point. I didn't think of that. I've amended my answer with a a hardlink version and an explanation of hardlinks too. (Since this question is basically all about links). – Petr Skocik Sep 30 '14 at 15:40
  • Won't git break the hardlinks ? (https://codingkilledthecat.wordpress.com/2012/08/08/git-dotfiles-and-hardlinks/, http://stackoverflow.com/questions/3729278/git-and-hard-links#3731139) – Jérôme Mar 24 '17 at 17:33