These are the steps to use git bare repositories to keep an auto-updating backup of all your dotfiles. These Instructions are divided into two parts:
- Setting up the git bare repository.
- Recovering from a git bare repository.
- Create a git bare repository in a "dotfiles" folder in the home directory.
git init --bare $HOME/.dotfiles
- Add an alias to your shell config (I am currently using Fish)
alias dfbu='/usr/bin/git --git-dir=$HOME/.dotfiles/ --work-tree=$HOME'
-
Reload your shell (Type fish or restart your terminal)
-
Run the following command to make sure you don't get harassed by untracked files:
dfbu config --local status.showUntrackedFiles no
And that's it. Now you can start using the normal git command like add, commit, etc. to handle the files you want to back up from any directory.
- Prior to installing/recovery, make sure the alias you made above has been committed to the shell config file.
alias dfbu='/usr/bin/git --git-dir=$HOME/.dotfiles/ --work-tree=$HOME'
-
Make sure your source repo ignores the folder where you'll clone it, to avoid any recursion problems.i.e. Add a an entry "dotfiles" (without quotes) to a file named ".gitignore" (without quotes). This .gitignore file will be placed in the $HOME directory.
-
Now clone your bare repo in a ".dotfiles" folder of your $HOME directory.
git clone --bare <insert-repo-url-here> $HOME/.dotfiles
- Define the alias dfbu in your current shell (of your new system)
alias dfbu='/usr/bin/git --git-dir=$HOME/.dotfiles/ --work-tree=$HOME'
- Checkout the actual content from the bare repo to your $HOME:
dfbu checkout
- The above message might fail with a message like:
error: The following untracked working tree files would be overwritten by checkout:
.bashrc
.gitignore
Please move or remove them before you can switch branches.
Aborting
This is because your $HOME folder might already have some stock configuration files which would be overwritten by Git. The solution is simple: back up the files if you care about them, remove them if you don't care. A rough shortcut is provided below to move all the offending files automatically to a backup folder:
mkdir -p .config-backup && \
config checkout 2>&1 | egrep "\s+\." | awk {'print $1'} | \
xargs -I{} mv {} .config-backup/{}
Now rerun step 5.
- Run the following command to make sure you don't get harassed by untracked files:
dfbu config --local status.showUntrackedFiles no
- That is it. You can now continue using your normal git commands using the dfbu alias as you have been doing previously. GG WP. Bye.
(This method has been an adaptation of the mothod shown in the following article: https://www.atlassian.com/git/tutorials/dotfiles)