The best way to manage dot files with Git is to use a "bare" repository. This is a special kind of repository that doesn't have a working directory, which is perfect because you don't want a .git folder cluttering up your home directory.
Here’s the simple, step-by-step process.
First, open your terminal and initialize a bare Git repository in a hidden directory.
cd ~
git init --bare $HOME/.dotfiles
Now, let's create a handy alias so you don't have to type a long command every time. This alias lets you use dotfiles as a command for managing your repository.
alias dotfiles='/usr/bin/git --git-dir=$HOME/.dotfiles/ --work-tree=$HOME'
Add this line to your shell's configuration file (like .bashrc or .zshrc) to make it permanent.
You need to tell Git to ignore all the other files in your home directory so git status doesn't get cluttered.
dotfiles config --local status.showUntrackedFiles no
Now you can add the specific dot files you want to track, just like you would with any other repository.
dotfiles add .bashrc .zshrc .vimrc
dotfiles commit -m "Initial commit of dot files"
Create an empty repository on GitHub, then link your local one to it and push your files.
dotfiles remote add origin [email protected]:your_username/your_repo_name.git
dotfiles push -u origin main
🎉 Your dot files are now backed up on GitHub!
On a new computer, the process is just as easy.
On the new machine, open your terminal and clone the bare repository into your home folder. This creates the hidden .dotfiles directory where Git will live.
git clone --bare [email protected]:your_username/your_repo_name.git $HOME/.dotfiles
Note: Replace the URL with your actual GitHub repository URL.
Define the alias that lets you manage your dot files easily. Add this to your shell's config file (e.g., .zshrc) after the setup.
alias dotfiles='/usr/bin/git --git-dir=$HOME/.dotfiles/ --work-tree=$HOME'
Pull your configurations into your home directory and tell Git to ignore all untracked files.
dotfiles checkout
dotfiles config --local status.showUntrackedFiles no
Now for the magic! Navigate into your .dotfiles directory and execute the setup script you wrote. This script will automatically install Homebrew, Oh My Zsh, install all your apps from the Brewfile, and finalize your system configuration.
cd $HOME
chmod +x install.sh
sh install.sh
That's it! Your perfect development environment is now just a few simple commands away, no matter what machine you're on.
Managing your dot files this way is a small change that can save you a ton of time and frustration in the long run. Happy coding!
Còn chút gì để nhớ
Bài cảm nhận tham gia cuộc thi "Trở lại học trò" do Nhà xuất bản Trẻ tổ chức nhân sự kiện ra mắt truyện dài "Lá nằm trong lá" 2011
My 2024 MacBook Pro Setup
I share how dotfiles made the migration simple, why I swapped iTerm2 for Ghostty, and how Catppuccin has become my new favorite theme. Plus, I reveal the candidates trying to replace my current browser.