我也在寻找一种以最少的步骤设置新机器的方法,在花了一些时间后,我发现几乎所有的开发人员都使用 git 来存储和共享这些文件和符号链接来同步它们。
好吧,符号链接有效,但它不是将本地文件同步到 git 存储库的最佳方式。有一个更好的解决方案,由 Atlassian 的人编写 – https://www.atlassian.com/git/tutorials/dotfiles。
因此,将文件与远程副本同步到 gitbare
存储库是最好和最优雅的方式,创建一个 bash 脚本来自动安装和设置。
管理点文件
管理这些点文件的技巧是创建一个裸 git 存储库。如果您从头开始并且之前没有跟踪您的点文件,请在 $HOME 目录中创建一个裸存储库。
git init --bare $HOME/.dotfiles
为了更容易使用,我们将其命名为 dotfiles,我们将使用它而不是常规 git 来与我们的 dotfiles 存储库进行交互。
alias dotfiles="/usr/bin/git --git-dir=$HOME/.dotfiles --work-tree=$HOME"
现在我们可以使用 dotfiles 命令跟踪我们的 dotfiles,其中一些示例是:
# to check the version history
dotfiles log
# to check the status of the tracked and untracked files
dotfiles status
# to add a file for tracking
dotfiles commit .vimrc -m ".vimrc added"
# push new files or changes to the github
dotfiles push origin main
点文件的用例和优势
这种管理和共享的方法有很多优点,下面列出了其中一些。
易于设置
设置新机器可能是一项耗时的任务,但通过这种方法,我们可以在一分钟内使用我们的个性化配置。我们只需要克隆存储库并获取 .bashrc 或 .zshrc 文件。
git clone --bare https://github.com/<username>/dotfiles.git $HOME/.dotfiles && source ~/.zshrc
版本化的点文件
最适合尝试新配置并保留更改历史记录(基本上所有使用 git 的优点)
# to check the version history
dotfiles log
在多个设备上共享
使用分支以最少的更改共享多个设备的相同配置,为您的新机器创建一个分支,例如:-
# Create configurations specific to your aws machines
dotfiles checkout -b aws
点文件的配置文件
使用分支根据您的环境创建配置,创建一个分支并根据您的工作环境进行配置。
# Manage multiple profiles - check out to the work profile
dotfiles checkout work
我也使用这种方式来同步和存储我的 dotfiles,查看我的 dotfiles 存储库,并且可以在Tooling is hard and necessary中阅读我写的关于管理多个设备的文章。