简体   繁体   中英

Best way to version control config files under home (~) with git

I would like to version control my config files and scripts under ~ such as .bashrc , .vimrc , .bash_profile etc.. but that would require a git init in the home folder which would interfere with other git repos below home and which will require adding all other files to .gitignore .

What would be the best way to do it? I was thinking of setting up a repo with all the config files in a dedicated folder and them symlinking them to their original locations but that would require extra steps to create symlinks for all the newly added files on all boxes where I need to sync and possibly synlinks might not work with all apps.

What would be the best approach?

The .gitignore supports patters, so you could use it to enumerate the files you want to control:

#Ignore all
/*

#But not these
!.bash*
!.vimrc
!etc...

That's all.

BTW, if git sees a sub-folder which has .git in it - the sub-folder is ignored.

I see at least two ways how to resolve the issue. Let's assume all repositories are stored under ~/mygits .

  1. This directory is explicitly dedicated as a container for all your git repositories and declared in .gitignore
~/.bash_profile
~/.bashrc
~/.git/*
~/.gitignore
~/mygits/**

Example of .gitignore

$ cat .gitignore
mygits
  1. put the target repository (let's call it as mydotfiles ) under ~/mygits next to other repositories ...
~/mygits/mydotfiles/bash_profile
~/mygits/mydotfiles/bashrc

... and create links to the files

ln -s mygits/mydotfiles/bash_profile ~/.bash_profile
ln -s mygits/mydotfiles/bashrc ~/.bashrc

In my opinion the second way is better because you don't need to think of preventing to put other stuff from homedir to this repository.

Finally, hope my explanations are clear.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM