Track GNU/Linux dotfiles with git — Robin Wils's website

Last modified: Tue, Aug 16, 2022

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.

# Dotfiles tracker
# Function which adds changed dotfiles to git stage
if [ ! -f $DOTFILES_FILE ]; then
    touch $DOTFILES_FILE

for dotfile in `cat $DOTFILES_FILE`; do
    git add -f $DOTFILES_FILE/../$dotfile

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 file to the git stage everytime you open a new shell. It should run every time you open up a new terminal.

You add file to the automatically tracked files by running the following commands:

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

Make sure to track the .gitignore file, so that it gets commited to git later. Check which dotfiles are tracked by running the following command 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