树洞

靡不有初,鲜克有终

Git 批量 checkout 和 update

一、下载脚本

脚本代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#!/bin/bash
branch="production"

if [ "$1" ]
then
branch="$1"
fi

for f in `ls ./`
do
if [[ ${f} == * ]]
then
echo -e "\n${f}"
cd ${f}

echo "1. checkout ${branch} result: "
git checkout ${branch}
echo "2. pull rebase result: "
git pull --rebase
cd ..
fi
done
echo "Finished"

将脚本放置在项目代码同一目录下。

二、赋权

进入当前目录,执行赋权命令:

1
chmod +x ./update.sh

三、运行

注意:执行前,记得 commit 或者 shelve 代码,以防本地代码丢失。

默认将所有仓库代码 checkout 到 production 分支,并执行 pull –rebase 命令:

1
./update.sh

如果需要 checkout 到其他分支,可以加上分支名,例如:

1
./update.sh master

四、配合 Alfred Workflow

创建 Blank Workflow,再创建一个 Inputs - Keyword 和 Actions - Run Script。
在 Keyword 和 Run Script 分别设置 Argument Optional 和 with input as {query}。
脚本代码,需要修项目目录:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#!/bin/bash

# 修改项目目录
cd /Users/IdeaProjects/

branch="production"
param={query}

if [ "$param" ]
then
branch="$param"
fi

for f in `ls ./`
do
if [[ ${f} == moego* ]]
then
cd ${f}
git checkout ${branch}
git pull --rebase
cd ..
fi
done