Skip to content

Instantly share code, notes, and snippets.

@NicholasasaurusRex
Last active December 4, 2023 19:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NicholasasaurusRex/b967b54394b02d54e464c9de8407352b to your computer and use it in GitHub Desktop.
Save NicholasasaurusRex/b967b54394b02d54e464c9de8407352b to your computer and use it in GitHub Desktop.
Manage your dot files with git:

GitImage

Manage your dot files with git:

I found an article in Hacker News on how to use git for dot file management. I found that it works well.

Start from nothing:

git init --bare $HOME/.dot
alias dot='/usr/bin/git --git-dir=$HOME/.dot/ --work-tree=$HOME'
dot config --local status.showUntrackedFiles no
echo "alias config='/usr/bin/git --git-dir=$HOME/.dot/ --work-tree=$HOME'" >> $HOME/.config/zsh/.zshrc

Here is whats going on:

  • This first command git init --bare $HOME/.dot creates the git repository. In this case a git bare repository.
  • Line two creates an alias dot which is used instead of git, and to save alot of typing.
  • The third one uses the alias we just created, and sets a flag to the local repository we just created. Then is hides all the files that we don't want to explicitly track.
  • Lastly, we are sending that alias we created to the .zshrc dot file, again to save alot of typing.

Now all you have to do is add files you want to track. Using the same commands you would with git. Except instead of using the command git you would replace it with the alias dot

Example:
dot status
dot add $HOME/.config/zsh/.zshrc
dot commit -m "Added .zshrc"
dot push origin dotfiles

Already started:

Make sure you already have your alias configured in zsh (or bash):

alias dot='/usr/bin/git --git-dir=$HOME/.dot/ --work-tree=$HOME'

Now, to ensure the source repository ignores the folder that it will be cloned into.

echo "dot" >> .gitignore

Using git and not the alias, clone the repository into a bare repositoryin the $HOME directory.

git clone --bare <git-repo-url> $HOME/.dot

Ensure the current shell you are on has the created alias:

alias dot='/usr/bin/git --git-dir=$HOME/.dot/ --work-tree=$HOME'

If you get an error:

error: The following untracked working tree files would be overwritten by checkout:
    .zshrc
    .gitignore
Please move or remove them before you can switch branches.
Aborting

This will be because the $HOME folder may have some original files from the old repository still left in it. Either delete them or back them up somewhere and then delete them.

Then re-run checkout

dot checkout

Set the flag to not show untracked files on this local repository.

dot config --local status.showUntrackedFiles no

That's it, dot file management with git.

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