Batch Git Pull:分享一个维护多个 Git 仓库的小脚本

不废话,放脚本:

find . -maxdepth 3 -name .git -type d | rev | cut -c 6- | rev | xargs -I {} git -C {} pull

更为方便的,直接将这部分加入你的 .zshrc 或者 .bash_profile

alias gpall="find . -maxdepth 3 -name .git -type d | rev | cut -c 6- | rev | xargs -I {} git -C {} pull"

之后,直接执行 gpall 即可。

好了,我们进入正题。

维护多个 Git 仓库的需求

维护多个 Git 仓库不容易。我在我存放 GitHub 仓库的目录下运行了一下 tree

.
├── AIP_BackEnd
├── Evaluation_BackEnd
| ... ...
├── SchoolProjects
│   ├── Distance-Vector-Algorithm
│   ├── cartoonize-images
| ... ...
│   ├── zanpress-blog
│   └── zanpress-diagram
| ... ...
└── wechat-format

103 directories

103 个目录……我自己 Documents/GitHub 文件夹下就有这么多 Git 仓库,一个一个去更新真的很费事情。如何批量更新 GitHub 本地仓库呢?其实就是一个遍历目录,对匹配到的 Git 仓库在其当前分支下执行 git pull 的需求嘛,很简单。

解决方法

在 Medium 上面,我找到了一个相当优雅的脚本。前面这个脚本已经分享给各位了,我们重新看一下:

find . -maxdepth 3 -name .git -type d | rev | cut -c 6- | rev | xargs -I {} git -C {} pull

可以发现,这一长串命令,事实上就是前面的命令执行结果通过「管道」输出至后面的命令作为输入,也就是命令中 | 的功能。我们一段一段看一下这个命令具体都干了什么。

find 搜索目录下全部 .git/ 文件夹

find . -maxdepth 3 -name .git -type d

裁剪出我们要的 .git 文件夹所在路径

... | rev | cut -c 6- | rev | ...

Screen Shot 2019-10-29 at 3.51.46 PM.png

利用 xargs 执行带参数的 git pull

xargs -I {} git -C {} pull

最后,我们来看一看效果:

Screen Recording 2019-10-28 at 11.39.57 AM.2019-10-28 11_45_45.gif

📚References


You'll only receive email when they publish something new.

More from Spencer Woo
All posts