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
Create a new folder
$HOME/dotfiles
and rungit init --bare
to initialize it as a bare repository (without a working directory) to track the configuration files you need to manageTransform 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 runcode $PROFILE
in PowerShell, open the configuration file using VS Code, and add the following function:ps1function gitbare { git --git-dir=$HOME/dotfiles --work-tree=$HOME $args }
After step 2, when we need to work on our dotfiles repository, we can use the retrofit
gitbare add
,gitbare commit
, andgitbare remote
instead of the originalgit add
,git commit
, andgit remote
Create a new repository on GitHub, https://github.com/user-name/dotfiles
shgitbare remote add origin https://github.com/user-name/dotfiles.git gitbare branch -M main gitbare push -u origin main
Now you can selectively add, commit, and push configuration files from your
$HOME
directory to your remote GitHub repository for backup
本地仓库从零重建
远程仓库已经存在,也包含之前推送过的仓库文件夹和文件。现在本地仓库删除,完全从零开始重建。当尝试运行 gitbare push
触发以下报错:
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
之后,旋即触发以下报错:
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...