Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symlink dotfiles

I am having trouble symlinking dotfiles. I have a folder in my home directory ~/dotfiles which I have synced to a github repo. I am trying to take my .vimrc file in ~/dotfiles/.vimrc and create a symbolic link to put it at ~/.vimrc. To do this I type in

ln -s ~/dotfiles/.vimrc ~/.vimrc

But when I run that it says

ln: /Users/me/.vimrc: File exists

What am I doing wrong?

like image 697
Dan Hessler Avatar asked Oct 02 '17 21:10

Dan Hessler


People also ask

What is dotfiles in Linux?

Dotfiles are configuration files for various programs, and they help those programs manage their functionality. What sets them apart from regular files and directories is their prefix. Dotfiles are named that way because each file and directory starts with a dot ( . )


1 Answers

There is a better solution for managing dotfiles without using symlinks or any other tool, just a git repo initialized with --bare.

A bare repository is special in a way that they omit working directory, so you can create your repo anywhere and set the --work-tree=$HOME then you don't need to do any work to maintain it.

Approach

first thing to do is, create a bare repo

git init --bare $HOME/.dotfiles

To use this bare repo, you need to specify --git-dir=$HOME/.dotfiles/ and --work-tree=$HOME, better is to create an alias

alias dotfiles='/usr/bin/git --git-dir=$HOME/.dotfiles/ --work-tree=$HOME

At this point, all your configuration files are being tracked, and you can easily use the newly registered dotfiles command to manage the repository, ex :-

# to check the status of the tracked and untracked files 
dotfiles status

# to add a file 
dotfiles commit .tmux.conf -m ".tmux.conf added"

# push new files or changes to the github
dotfiles push origin main

I also use this way to sync and store my dotfiles, see my dotfiles repository and can read at Storing dotfiles with Git where I wrote about managing for multiple devices.

like image 162
Manish Sahani Avatar answered Oct 07 '22 04:10

Manish Sahani