Skip to content

Instantly share code, notes, and snippets.

@stiffneckjim
Last active December 26, 2025 20:32
Show Gist options
  • Select an option

  • Save stiffneckjim/bb5955a09247caae9f30c6a98097fbf4 to your computer and use it in GitHub Desktop.

Select an option

Save stiffneckjim/bb5955a09247caae9f30c6a98097fbf4 to your computer and use it in GitHub Desktop.
Storing dot files in a git repository

Aims

  1. Keep dot files in a code repository so I can track changes and make notes
  2. Share dot files across machines and environments
  3. e different files for different operating systems

Tools

Git or Git for Windows

Create the repo

Start in your home directory and create a bare git repo. Git creates the directory for us.

  % cd ~
  % git init --bare $HOME/.myconf
  Initialized empty Git repository in ...

Use an alias for the new repo

Now we're going to use a clever alias so that the command config will allow us to make changes to the new repo even though the files aren't in the folder we just created. I love this. I'm using which git because it's different depending on my OS.

  % alias config='$(which git) --git-dir=$HOME/.myconf/ --work-tree=$HOME'

Check it works

  % config status
  A whole lot of files not in the repo...

Now we use the new config alias to stop seeing warnings about untracked files

  % config config status.showUntrackedFiles no  

Check it works still

  % config branch -m main
  % config status
  On branch main

  No commits yet

  nothing to commit (create/copy files and use "git add" to track)

Beautiful, so now I can try adding dot files with config add. I'll start with .zshrc and then add the alias for config and try committing the change.

  % config add .zshrc
  % config status
  ...
         new file:   .zshrc
  ...
  % config commit -m "This is the first version of my .zshrc file."

Branching for changes

We've added our first file to the master branch but one of the cool tricks for this approach is having a branch for each change for each computer. So before we go, let's create a branch and then add our alias change to it.

  % config branch [computername]/add_alias_for_config
  % config checkout [computername]/add_alias_for_config

Now time to add the alias for config so it's loaded when the shell starts. I'm using ZSH so I've added this to the end of .zshrc file

  # Aliases
   alias config='$(which git) --git-dir=$HOME/.myconf/ --work-tree=$HOME'

Check it works

  % source .zshrc

And commit it to the repo

  % config add .zshrc
  % config commit -m "Added an alias for config command to use a git repo for storing my dot files."

If we're happy it's all working as expected, we can push the change back to the main branch and delete the new branch.

  % config checkout main
  % config merge [computername]/add_alias_for_config
  % config branch -d [computername]/add_alias_for_config

Push repo to GitHub

Now to push it somewhere. I'm assuming GitHub. First, make sure your local git is configured. Check show your user name.

  % git config --global user.name

Check your email address. If you're using GitHub this should probably be anonymous email ending ...@users.noreply.github.com

  % git config --global user.email

Next push this to GitHub

  % config remote add origin git@github.com:[username]/myconf.git
  % config pull --rebase origin master git push origin master

Add another computer

You can't add files straight away (I tried) but the Internet showed me an alternative. Clone the repo in a temporary directory.

  % git clone --separate-git-dir=$HOME/.myconf git@github.com:[username]/myconf.git $HOME/myconf-tmp

Then set up the alias used earlier

  % alias config='$(which git) --git-dir=$HOME/.myconf/ --work-tree=$HOME'
  % config config status.showUntrackedFiles no

Check and you should get a status for the files you have stored in GitHub

  % config status
  On branch main
  Your branch is up to date with 'origin/main'.

  Changes not staged for commit:
    (use "git add/rm <file>..." to update what will be committed)
    (use "git restore <file>..." to discard changes in working directory)
        modified:   .zshrc
    no changes added to commit (use "git add" and/or "git commit -a")

You can now delete the temporary directory.

  % rm -r $HOME/myconf-tmp

We can restore the missing files.

  % config restore .gitignore

For files we want to check we can branch, then merge and decide which changes to keep.

  % config branch [computername]_wsl/initial_version
  % config checkout [computername]_wsl/initial_version
  % config diff .zshrc

Now edit the file and commit your changes. When you're done you can commit the changes and push them back to main and GitHub.

Sources

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment