Elzohairy

Share this post

Dotfiles Management

elzohairy.substack.com

Discover more from Elzohairy

Life
Continue reading
Sign in

Dotfiles Management

A Step-by-Step Guide to Using Bare Git Repositories to Manage Dotfiles

Mohamed El-Zohairy
Apr 14, 2023
Share this post

Dotfiles Management

elzohairy.substack.com
Share

This approach was first described by StreakyCobra on Hacker News. It uses a bare Git repository in the user's home directory to manage and version control dotfiles directly without needing symbolic links or a separate directory for dotfiles.

Here's a step-by-step explanation of the process:

Step 1: Create a bare Git repository

Run `git init --bare $HOME/.myconf` in the Terminal. This command initializes a new bare Git repository at `~/.myconf`. A bare repository does not have a working tree, which means it only contains the Git history and not the actual files.

Step 2: Set up an alias for the Git command

Run `alias config='/usr/bin/git --git-dir=$HOME/.myconf/ --work-tree=$HOME'` in the Terminal. This command creates an alias called `config` that maps to the Git command with the specified `--git-dir` (the location of the Git repository) and `--work-tree` (the location of the actual files) options. By using the `config` alias, you can run Git commands directly on files within your home directory.

Step 3: Configure the repository

Run `config config status.showUntrackedFiles no` in the Terminal. This command tells Git not to show untracked files when running `config status`. This is useful to avoid clutter in the status output since many files in your home directory may not be relevant to your configuration.

Step 4: Use the `config` alias to manage your dotfiles

Now you can use the `config` alias like you would use the `git` command to manage your dotfiles:

- `config status`: Check the status of your dotfiles.

- `config add .vimrc`: Add the `.vimrc` file to the repository.

- `config commit -m "Add vimrc"`: Commit the added file with a commit message.

This approach lets you manage and version control your dotfiles directly within your home directory without needing to create symbolic links or manage a separate dotfiles directory.

Step 5: Setup a remote repo on Github and link your local repo to it

  • Go to https://github.com and sign in (or sign up if you don't have an account).

  • Click the "+" icon in the top-right corner and select "New repository."

  • Name your repository, e.g., "dotfiles" and provide a description (optional).

  • Choose whether you want the repository to be public or private.

  • Click "Create repository."

  • In the Terminal, run the following command to add the remote repository to your local repository (replace `yourusername` with your GitHub username and `dotfiles` with your repository name):

    config remote add origin https://github.com/yourusername/dotfiles.git
  • Finally, push your changes to the remote repository

    config push -u origin main 

Step 6: Get the dotfiles on a different machine

To replicate this setup on a new machine, you can clone the bare repository into a new `.myconf` directory and set up the `config` alias :

git clone --bare <remote-repo-url> $HOME/.myconf
alias config='/usr/bin/git --git-dir=$HOME/.myconf/ --work-tree=$HOME'
config config status.showUntrackedFiles no
config checkout

If you encounter any issues with the checkout step, such as conflicts with existing files, you can backup the conflicting files, delete them, and run `config checkout` again.

Thanks for reading Elzohairy! Subscribe for free to receive new posts and support my work.

Share this post

Dotfiles Management

elzohairy.substack.com
Share
Comments

Ready for more?

© 2023 Mohamed El-Zohairy
Privacy ∙ Terms ∙ Collection notice
Start WritingGet the app
Substack is the home for great writing