通过dotbot管理dotfiles并搭建dotfiles仓库。
为什么选择Dotbot?
不需要自己编写dotfiles配置文件和相关脚本,更方便使用和管理。
dotbot特点如下:
- 只需要一个配置文件(
install.conf.yaml
) - 只需要一行命令即可在不同设备安装符号链接
- 可以作为git submodule使用
使用Git添加子模块Dotbot
mkdir ~/.dotfiles # Create project directory
cd ~/.dotfiles # replace with the path to your dotfiles
git init # initialize repository if needed
git submodule add https://github.com/anishathalye/dotbot
git config -f .gitmodules submodule.dotbot.ignore dirty # ignore dirty commits in the submodule
cp dotbot/tools/git-submodule/install .
touch install.conf.yaml
现在开始编辑配置文件install.conf.yaml
- defaults:
link:
create: true
relink: true
- clean: ['~']
- link:
~/.zshrc: zshrc
~/.vimrc: vimrc
- shell:
- [git submodule update --init --recursive, Installing submodules]
Default options for plugins can be specified so that options don’t have to be repeated many times.
Defaults apply to all commands that come after setting the defaults. Defaults can be set multiple times; each change replaces the defaults with a new set of options.
Default: 这里设置了create和relink两个参数,更多参数信息参考这里。
Parameter | Explanation |
---|---|
create | When true, create parent directories to the link as needed. (default: false) |
relink | Removes the old target if it’s a symlink (default: false) |
Clean commands specify directories that should be checked for dead symbolic links. These dead links are removed automatically. Only dead links that point to somewhere within the dotfiles directory are removed unless the
force
option is set totrue
.
Clean: 根据需求设置即可。
Link commands specify how files and directories should be symbolically linked.
Link: 这是经常修改的地方,定义如何链接我们的dotfiles。这里以~/.zshrc
和~/.vimrc
举例。
Shell commands specify shell commands to be run. Shell commands are run in the base directory
在这里是安装submodules。
将dotfiles移至仓库
这里以~/.zshrc
和~/.vimrc
举例。
cp ~/.vimrc ./vimrc
cp ~/.zshrc ./zshrc
接着可以删除
~/
下的.vimrc
和.zshrc
文件。(⚠️注意备份)
运行安装脚本
现在我们可以通过./install
测试是否正常运行。正常情况下将会看到类似👇的信息:
./install
All targets have been cleaned
Creating link ~/.zshrc -> ~/.dotfiles/zshrc
Creating link ~/.vimrc -> ~/.dotfiles/vimrc
All links have been set up
Installing submodules [git submodule update --init --recursive]
All commands have been executed
==> All tasks executed successfully
commit + push -> 远程仓库
假设在GitHub上创建了名为dotfiles的仓库
git add --all
git remote add origin git@github.com:username/dotfiles.git
# git remote -v 查看是否更新remote repo信息
# git pull origin main 如果有需要拉取到本地的文件,则将远程分支与当前分支合并
git push -u origin main
在其他机器上使用仓库里的配置文件
git clone git@github.com:username/dotfiles.git --recursive
cd dotfiles && ./install
如果仓库有更新则可通过下面命令更新本地配置:
git pull
./install
总结
通过Dotbot和Git可以方便我们维护dotfiles并通过Git仓库共享配置。