Skip to content

Backup dotfiles on GitHub

There might be many tools installed on your computer; each has a config file, stored at different locations. What if you just purchased a new computer, or simply re-installed OS on your existing computer? Do you want to set up your config files from scratch all over again?

Goal

  • Manage and backup various configuration files on your computer through Git
  • Share the same set of dotfiles between two different machines
  • System:
    • Windows 10, PowerShell 7.5.3 Preview
    • MacOS, iTerm2
  • Method: Through a bare Git repository

Tracked files

  • .zshrc
  • AppData/Local/nvim

Backup

  1. Create a new folder $HOME/dotfiles and run git init --bare to initialize it as a bare repository (without a working directory) to track the configuration files you need to manage

  2. Transform the basic git command into the gitbare command, i.e. when running gitbare in PowerShell instead of basic git, Git automatically treats $HOME/dotfiles as the repository and the entire $HOME directory as the workspace. To accomplish this substitution, simply run code $PROFILE in PowerShell, open the configuration file using VS Code, and add the following function:

    ps1
    function gitbare {
    git --git-dir=$HOME/dotfiles --work-tree=$HOME $args
    }
  3. After step 2, when we need to work on our dotfiles repository, we can use the retrofit gitbare add, gitbare commit, and gitbare remote instead of the original git add, git commit, and git remote

  4. Create a new repository on GitHub, https://github.com/user-name/dotfiles

    sh
    gitbare remote add origin https://github.com/user-name/dotfiles.git
    gitbare branch -M main
    gitbare push -u origin main
  5. Now you can selectively add, commit, and push configuration files from your $HOME directory to your remote GitHub repository for backup

本地仓库从零重建

远程仓库已经存在,也包含之前推送过的仓库文件夹和文件。现在本地仓库删除,完全从零开始重建。当尝试运行 gitbare push 触发以下报错:

sh
fatal: The current branch main has no upstream branch.
To push the current branch and set the remote as upstream, use

    git push --set-upstream origin main

To have this happen automatically for branches without a tracking
upstream, see 'push.autoSetupRemote' in 'git help config'.

根据提示,运行 gitbare push --set-upstream origin main 之后,旋即触发以下报错:

sh
error: src refspec main does not match any
error: failed to push some refs to 'https://github.com/alowree/dotfiles.git'

没有更好的办法,只好手动删除运程仓库,再重新推送。

Pull

To be tested...

Reference

最近更新