Home > Articles > Easily track your dotfiles with git

Easily track your dotfiles with git - Robin Wils's website

Last modified: Tue, Dec 31, 2019

Table of Contents

Creating the git repository

The first thing I did was create a new git repository in my home directory with a .gitignore file which ignores everything. I don’t want to track all my files. You can do this by running the following commands.

git init
touch .gitignore
echo '*' > .gitignore

Creating a function to track the dotfiles

I want to track my dotfiles, so I created a function in my .zshrc (.bashrc for bash users).

The second part of the script just runs the function. This automatically tracks the changed dotfiles after starting a new bash or zsh session.

# function which adds changed dotfiles to git stage
track-dotfiles () {
		for dotfile in `cat dotfiles_to_track`; do
				git add -f $dotfile
		done
}

# Run dotfiles function if the dotfiles_to_track file exists
if [ -f dotfiles_to_track ]; then
	 track-dotfiles
fi

Make sure to reload your .rc file after creating this function.

# Bash users
source .bashrc

# Zsh users
source .zshrc

Using the function to track the dotfiles

The created function adds every location which is in the dotfiles_to_track file to the git stage.

You can track a file by running the following commands:

# Add location or file to dotfiles_to_track file
echo '.zshrc' >> dotfiles_to_track
# Example 2: echo '.config/sway' >> dotfiles_to_track

# Run track-dotfiles function to add the dotfiles to the git stage
track-dotfiles

Check which dotfiles you are track by running the following in your home directory:

git status

You can simply commit and push your changes after doing that.

git remote add origin <HTTPS or SSH url of your new git repository>

git commit -m "Your commit message"

git push

Enjoy!

Home > Articles > Easily track your dotfiles with git