This is an idea that I saw on DistroTube's channel.
I followed instructions from here
- Create a new bare Git repo to store the history for your dotfiles.
git init --bare $HOME/dotfiles- Add this alias to your shell configuration file. This will allow you to use the
dotgitcommand to interact with your dotfiles repo.
# zsh
echo 'alias dotgit="git --git-dir=$HOME/dotfiles/ --work-tree=$HOME"' >> $HOME/.zshrc
source $HOME/.zshrc
# zsh with $XDG_CONFIG_HOME Set
echo 'alias dotgit="git --git-dir=$HOME/dotfiles/ --work-tree=$HOME"' >> $XDG_CONFIG_HOME/.zshrc
source $XDG_CONFIG_HOME/.zshrc
# bash
echo 'alias dotgit="git --git-dir=$HOME/dotfiles/ --work-tree=$HOME"' >> $HOME/.bashrc
source $HOME/.bashrc- Add this function to your shell configuration file. This adds a check to rsync that will warn if an overwrite is going to occur - This will be useful later when you are configuring a new machine.
function dotfiles_rsync {
if [ "$#" -lt 2 ]; then
echo "Usage: dotfiles_rsync <rsync-options> <source> <destination>"
return 1
fi
# Extract the last two arguments as source and destination
SOURCE="${@: -2:1}"
DESTINATION="${@: -1}"
RSYNC_OPTIONS=("${@:1:$#-2}") # All arguments except the last two
# Dry-run
OUTPUT=$(rsync -av --dry-run --itemize-changes "${RSYNC_OPTIONS[@]}" "$SOURCE" "$DESTINATION")
if echo "$OUTPUT" | grep -q '^>f.*'; then
echo "The following files will be overwritten:"
echo "$OUTPUT" | grep '^>f.*'
read -r -p "Are you sure you wish to continue? (y/n): " CONFIRM
if [[ "$CONFIRM" != "y" ]]; then
echo "Operation aborted."
return 1
fi
fi
# Actual sync
rsync -av "${RSYNC_OPTIONS[@]}" "$SOURCE" "$DESTINATION"
}- Tell Git that this repo should not display all untracked files (otherwise git status will include every file in your home directory, which will make it unusable).
dotgit config status.showUntrackedFiles no- Setup a remote repository to push your dotfiles to.
dotgit remote add origin git@github.com:GregOwen/dotfiles.git- To add a new dotfile to your Git repo, use your aliased Git command with your special options set.
dotgit add ~/.gitconfig
dotgit commit -m "Git dotfiles"
dotgit push origin master- Clone the repo onto a new machine as a non-bare repository; this will pull a snapshot of the dotfiles into
dotfiles-tmp
git clone \
--separate-git-dir=$HOME/dotfiles \
git@github.com:SparkleHazard/dotfiles.git \
dotfiles-tmp- Copy the snapshot from your temporary directory to the correct locations on your new machine.
dotfiles_rsync --recursive --verbose --exclude '.git' dotfiles-tmp/ $HOME/- Clean up
rm -rf dotfiles-tmp