Keeping your $HOME in Git, the right way

Posted on 2014-12-17.
This tutorial describes how you can keep your home directory in a Git repository.

So a lot of interesting websites on the Internet are talking about the benefits and pitfalls of keeping your entire $HOME directory in Git. As with most technology there are many ways to accomplish the same goal, but some are better than others.

Some are using makefiles with a lot of symbolic links, others are using a huge .gitignore which results in an unmaintainable nightmare, others are trying to script themselves out of the problems, and again some end up giving up.

But there actually exists a really easy and simple way to accomplish this.

First of all, it makes no sense at all to keep your entire $HOME in a Git repository. Only the relevant files in your $HOME should be put in Git. But how do we accomplish that while at the same time not having to maintain a huge .gitignore with a bunch of files to ignore (that we have to remember to update each time an unimportant file gets added to our $HOME)? And what about other Git repositories that resides in our $HOME directory as well?

Here's how you can accomplish your goal. Create a .gitignore in your $HOME directory and let it only contain a single asterisk sign *.

$ cd
$ cat .gitignore
*

The asterisk sign in .gitignore makes Git ignore everything by default, which is exactly what we want. Then we just add stuff manually from $HOME into Git with the -f (force) option:

$ git init
$ git add -f foo.txt
$ git add -f bar.txt
$ git commit -a -m "Add two new files"

Now the files foo.txt and bar.txt" has been added to our $HOME Git repository and everything else has been ignored.

The benefit of this approach is:

You just have to remember to add files manually using the -f options and then commit changes when needed.

This is a really simply, yet powerful way to keep your $HOME directory in Git.

So, how do you clone your $HOME directory from your bare repository server to a new box?

Let's say you're using some remote SSH server as your bare git repository (called home.git)

$ git clone --bare . home.git

You then need to upload that to your SSH server.

Then you add that as a remote server (the name my_repo is just an example, call it whatever you like):

$ git remote add my_repo ssh://foo@bar.com/home/foo/repo/home.git

Now you can push and pull to the remote repository from your $HOME directory.

What if you have just finished installing your favorite Linux or BSD on a new box and of want a copy of your $HOME on this new machine. You cannot simply start cloning.

This will NOT work:

$ cd
$ git clone ssh://foo@bar.com/home/foo/repo/home.git

Git will complain with:

fatal: destination path '.' already exists and is not an empty directory.

This makes sense.

And you cannot do a "pull" either as Git will complain about overwriting files - if similar files already exists.

What you need to do on the new box is this:

$ cd
$ git init
$ git remote add my_repo ssh://foo@bar.com/home/foo/repo/home.git
$ git fetch my_repo master
$ git reset --hard my_repo/master

Now you can push and pull on that box too.

That's it!

Feel free to email me any suggestions or comments.