Git Separated Worktree
- managing configuration files into your home directory
- any situation where you don’t want anyone or anything mess up your .git directory
bare repositories
- git allows to create bare repositories (i.e. directories including only the git files and no working directory) with the following command in an empty directory.
git init --bare
- any repository path which does not end with “/.git” is considered a bare repository.
cloning
using the following syntax it is possible to separate the working directory
looking into work directory we can find a plain text file named
git clone <repository> --separate-git-dir <git-dir> <work-dir>
.git
which contains the following
gitdir: <path/to/repo/dir>
setting up a git repository for an existing directory
if the directory exists already we can follow this procedure:
- create the git repository in a different place e.g.
cd ~/my_git_repos/ git init --bare new_repo
- add a .git file in the existing directory with the following text
gitdir: ~/my_git_repos/new_repo
- create a .gitignore file including all files and subdirectories we don’t want to include
- add the rest of the files to the index
HOME directory
one specific use case is configuration files in the home directory: this solution comes with a couple of warnings:
- you may risk to add personal files to a repo, or files not intended to be saved
- all subdirectories are seen as part of the repository
- move the .git file or rename it when you are not using the repository
- leave no bare files in your home directory; always use at least one subdirectory, so you can add it to gitignore