61

I have seen a few people that have git repos with their dot files. I'm wondering if they just

cd ~/
git init
git add .vimrc
// etc

? And then that's how they keep it up to date? Or do they probably make copies and sync them?

What strategy do you guys recommend or use? Mostly don't wanna commit and push my entire ~/

Thanks

John Tomson
  • 1,411
  • 2
  • 13
  • 14

5 Answers5

80

Making a git repository of your home is probably a bad idea (you would be spending more time creating your .gitignore file than on doing what you really want to do).

I suggest using a separate git directory for your dotfiles (eg. ~/git/dotfiles) and them making symlinks to your home (eg. ln -s ~/git/dotfiles/.vim ~/.vim, etc.).

If you can't be bothered creating symlinks manually each time you want to install your dotfiles somewhere, you can use a script like the following one: https://github.com/gibfahn/dot/blob/master/link.

sitaktif
  • 1,594
  • 13
  • 15
  • 28
    This solution works fine. But you don't have to spend an inordinate amount of time managing your `.gitconfig`. You can instead ignore everything `./*` ignore everything `!.vimrc` don't ignore `.vimrc` – Sethish Aug 13 '13 at 12:21
  • 1
    @Sethish, Works beautifully. I think yours is the best/cleanest answer. If you make it an answer I'll give it a +1 to make it more noticeable! – Jon49 Jan 09 '14 at 18:48
  • I agree that making a git directory in your homedir is a bad idea, but the above approach gets irritating if you start trying to include .bash_profile and your other dot files. You can end up with "too many levels of symbolic links" errors too easily, and the script for creating them that you link to is also a little prone to failure (has dependencies like greadlink). If the goal is really to save trouble and your focus is really on vim, then an approach like @romanil uses below would be ideal. It's not necessarily a bad answer, but it may not be the friendliest approach for most users. – jonnybot Apr 15 '14 at 18:18
  • 2
    Another interesting tool is this one: https://github.com/thoughtbot/rcm You can easily install it locally with `./configure --prefix=~/git/dotfiles` – samvv Oct 28 '14 at 12:35
  • 2
    If you do go down the git in homedir route, and ignore everything with `./*` make sure you reconfigure your global `.gitignore` so it doesn't clash with all your git repos afterwards: `git config --global core.excludesfile ~/.gitignore-global` (or something along those lines) – waffl Feb 29 '16 at 15:05
  • Does it matter which way you create the symbolic link? i.e., if the original vimrc is the one in your home directory and the "fake" vimrc is the one in your git version-controlled directory? – Niko Bellic May 02 '16 at 05:18
61

I have my ~/.vim directory under version control and my "real" vimrc (the one with all my settings) inside that directory, at ~/.vim/vimrc:

~/
---- .vim/
---- ---- (plugins and stuff)
---- ---- vimrc
---- .vimrc

My regular ~/.vimrc has only one line:

runtime vimrc

No need to create symlinks or whatever.

This is how I would push my config on a new machine where Git has already been installed:

$ cd
$ git clone git@github.com:romainl/dotvim.git .vim
$ echo "runtime vimrc" > .vimrc

What follows is the whole creation process. I assume that you have created an account and a repo named "vimconfig" on Github and that you already have a lovingly crafted ~/.vimrc and a well organized ~/.vim/.

$ cd
$ mv .vimrc .vim/vimrc
$ echo "runtime vimrc" > .vimrc
$ cd .vim
$ git init
$ echo "This is my Vim config." > README
$ git add *
$ git commit -m "My Vim config is versioned."
$ git remote add origin https://github.com/username/vimconfig.git
$ git push origin master

At that point, you should have the same content on Github and in your local repository.

You manage that repository normally and push your commits when you are ready. Simple.

Note that the whole Github thing is only useful if you need/want to sync your config on multiple machines or, somehow need/want to share it with others. If you don't, there's no real point using GitHub at all.

(edit)

Vim 7.4 introduced a new, very useful, scheme: it looks for the usual ~/.vimrc and also for ~/.vim/vimrc so that's even less work for you:

$ cd .vim
$ git init
$ echo "This is my Vim config." > README
$ git add *
$ git commit -m "My Vim config is versioned."
$ git remote add origin https://github.com/username/vimconfig.git
$ git push origin master

Of course, the strategy I suggested initially is still valid if you have to deal with mixed Vim versions: Vim knows what to do and won't crap out in an endless loop.

romainl
  • 186,200
  • 21
  • 280
  • 313
4

I already have a "bundle" folder where I put all my plugins to load via Pathogen. So I made that folder my git folder(with the plugins as submodules) and put my vimrc there.

In the official .vimrc I write:

let $MYVIMRC="<path_to_vimrc_in_bundle_folder>"
source $MYVIMRC
...
call pathogen#infect()

and I don't version it!

This allows me to share my configuration between my Linux machine at home and my Windows box at work. I can also put in the official .vimrc configurations specific to the machine, or ones I don't want to make available publicly(like the dbext profiles that contains logins and passwords...)

Idan Arye
  • 12,402
  • 5
  • 49
  • 68
2

Here is a platform agnostic and shell agnostic solution with no dependancies other then the bourne compatible shell you have available(bash, zsh, ksh, etc). It works on mac, linux, & windows:

dotsys

It will automatically synchronize changes to your dotfiles with github and multiple machines, along with many other features.

Arctelix
  • 4,478
  • 3
  • 27
  • 38
1

Check out StreakyCobra's solution in Hacker News:

git init --bare $HOME/.myconf
alias config='/usr/bin/git --git-dir=$HOME/.myconf/ --work-tree=$HOME'
config config status.showUntrackedFiles no

where ~/.myconf is the location of the bare git repository. Then any files within $HOME can be versioned like so:

config status
config add .vimrc
config commit -m "Add vimrc"
config add .config/redshift.conf
config commit -m "Add redshift config"
config push

If you need further explanation, Nicola Paolucci provided a great explanation.

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
Ian Canino
  • 21
  • 3