This guide explains how to use a bare Git repository to manage your dotfiles (configuration files in your home directory).
A bare Git repository is a repository that doesn't have a working directory. This means it doesn't have a copy of your files checked out. It only contains the Git data, which is usually stored in the .git directory.
For managing dotfiles, a bare repository is ideal because you can have your dotfiles in your home directory without having a .git subdirectory and without having to move your files into a versioned directory.
-
Initialize the bare repository:
git init --bare $HOME/.dotfilesThis creates a directory named
.dotfilesin your home directory, which will store your versioned dotfiles' history. -
Create an alias for easier use:
Add the following alias to your shell's configuration file (e.g.,
.bashrc,.zshrc):alias dotfiles='/usr/bin/git --git-dir=$HOME/.dotfiles/ --work-tree=$HOME'
This alias allows you to run git commands for your dotfiles repository from anywhere by using
dotfilesinstead ofgit. -
Reload your shell configuration:
source ~/.bashrc # or ~/.zshrc
-
Configure the repository to not show untracked files:
By default, the repository will show all files in your home directory as untracked. To change this, run:
dotfiles config --local status.showUntrackedFiles no
-
Add files to the repository:
Now you can start adding your dotfiles to the repository.
dotfiles add .bashrc dotfiles add .vimrc dotfiles add .config/nvim/init.vim
-
Commit your files:
dotfiles commit -m "Add initial dotfiles" -
Push to a remote repository (optional but recommended):
Create a new repository on a service like GitHub or GitLab and then push your local repository to it.
dotfiles remote add origin <remote_repository_url> dotfiles push -u origin master
To add new dotfiles, use the dotfiles add command:
dotfiles add .tmux.conf
dotfiles commit -m "Add tmux configuration"
dotfiles pushTo set up your dotfiles on a new machine:
-
Clone the repository:
git clone --bare <remote_repository_url> $HOME/.dotfiles
-
Add the alias to your shell's configuration file as described above.
-
Reload your shell configuration.
-
Checkout the files:
dotfiles checkout
If you get an error about your local files being overwritten, you can either back them up or force the checkout:
dotfiles checkout -f
-
Configure the repository to not show untracked files:
dotfiles config --local status.showUntrackedFiles no
Now your dotfiles are managed by your bare repository on the new machine.