寫支 Shell Script 處理 Git Clone 後重複的操作


#Mac#Linux#Git#Shell Script

公司有許多專案,git 倉儲 (repository) 有分正式與測試機,所以每次 clone 一個專案後都需要 git remote 去新增遠端倉儲位置,避免久久一個新專案 clone 下來後忘記做這動作,寫了一隻 shell script。

Shell Script 內容


建立一個檔案 git-clone.sh

vim git-clone.sh
#!/bin/sh
git clone git@test-domain.tw:/home/web/repos/$1.git
cd $1
git remote set-url --add --push origin git@test-domain.tw:/home/web/repos/$1.git
git remote set-url --add --push origin git@prod-domain.tw:/home/web/repos/$1.git

加上執行權限

chmod +x git-clone.sh

完成


mtsung@MTsung-Toby-MacBook twdd % ./git-clone.sh project_name
Cloning into 'project_name'...
remote: Enumerating objects: 7804, done.
remote: Counting objects: 100% (7804/7804), done.
remote: Compressing objects: 100% (7045/7045), done.
Receiving objects: 100% (7804/7804), 4.42 MiB | 1.10 MiB/s, done.
remote: Total 7804 (delta 5229), reused 830 (delta 466), pack-reused 0
Resolving deltas: 100% (5229/5229), done.
mtsung@MTsung-Toby-MacBook twdd % cd project_name
mtsung@MTsung-Toby-MacBook project_name % git remote -v
origin git@test-domain.tw:/home/web/repos/project_name.git (fetch)
origin git@test-domain.tw:/home/web/repos/project_name.git (push)
origin git@prod-domain.tw:/home/web/repos/project_name.git (push)