Backup dotfiles to GitHub
Or any other version control system

Introduction
This article is about how to back up dotfiles using Git. I will try to keep it short and straightforward.
I’m using Zsh
as the shell in the examples, but another shell can be used. The commands below should be executed as a normal user in the /home
directory.
Set up the repository
Create a local Git repository that will be used to track the dotfiles.
|
|
Prepare the environment
The default behaviour of Git commands is to run inside the project folder using information stored at the <project>/.git
directory and Git assumes that the working tree is located at <project>
. To enable the execution of Git commands specifically for the “dotfiles” repository from anywhere, it is needed to create an alias that indicates the location of the Git directory and the working tree.
|
|
Make this alias permanent.
|
|
Source the aliases
file inside the .zshrc
file by adding the following line.
|
|
Configure Git
By default, Git will consider all the files under the working tree as untracked. To avoid this behaviour, configure Git to show only the files that are explicitly added.
|
|
Add files to be tracked
Add the files that you want to track, for example.
|
|
Configure the remote URL
Link the local to the remote repository. The remote repository can be a GitHub or any other Git repository.
|
|
Push the changes to the remote repository
Push the changes to the remote repository. This is effectively the backup of the dotfiles.
|
|
Now the dotfiles are backed up to the remote repository.
Restoring the files
To restore the dotfiles in a new system, clone the repository using the --bare
option.
|
|
Before checking out, ensure that none of the repository files exist in the local filesystem. If there are existing files, backup them by renaming them to something like filename.old
and then run the following command.
|
|
Close the terminal and re-open it. All the configurations should be enabled now as the dotfiles should be at the right places.
Conclusion
Job done! The files are now being backed up to GitHub and restored to the right place. Remember to use the dotfiles
alias when dealing with the dotfiles Git repository.