我會做這樣的事情:
# fetch updates, but don't merge yet
git fetch # origin is default
# list all branches
git for-each-ref --format='%(refname:short)' refs/heads |
while read branch; do
# if the branch is not the same as the remote branch...
if [ "$(git rev-parse $branch)" != "$(git rev-parse origin/$branch)" ]; then
# only continue to the next step on successful results
git checkout $branch &&
git pull &&
(make &&
cp executable /somewhere/else;
make clean)
# except always make clean as long as we tried to make
#
# you might also consider a hard reset and clean, to guarantee things
# are safe for the next checkout
fi
done
約先取出的好處是,你不必不必要的檢查分支,沒有變化;如果你的回購很大,這可以節省時間。
顯然有處理錯誤的選擇;我只是用最簡單的東西去做,但仍然有意義。對於更復雜的處理,你可能反而想做的形式if make; then ...; else ...; fi
,例如事情:
# die hard on unexpected checkout/pull failures
if ! (git checkout $branch && git pull); then
exit 1
fi
if make; then
cp executable /somewhere/else
else
# report a build failure?
fi
# clean up no matter what
git clean -xdf
git reset --hard
附註:如果你不是想測試你的'使clean'規則,'混帳clean'的完整性(可能帶有選項'-x -d -f';請參閱聯機幫助頁)可用於清除未跟蹤的所有內容,以確保您的構建真正處於清理狀態。 – Cascabel
感謝你的這一點,我認爲git clean可能在這裏有很大的意義。 –