【发布时间】:2017-10-09 13:27:02
【问题描述】:
我正在编写一个小的 bash 脚本,如下所示:
#if you add a project you must use the format git clone https://<username>:<passsord>@github.com/ZiathLtd/<project-name>.git
#projects[1]=samples
#projects[2]=TubePicker
#projects[3]=FloydDriver
#projects[4]=Handheld
projects[1]=handheldserver
#projects[6]=ziathscripts
#projects[7]=handheldscripts
basedir="/mnt/raid/git"
for i in ${projects[@]}; do
echo 'updating' ${i}
cd ${basedir}/${i}
for b in `git branch -r | grep -v -- '->'`; do
echo working on ${b}
git rev-parse --verify ${b}
if [ $? -ne 0 ]
then
echo 'new branch'
git branch --track ${b##origin/} $b;
fi
done
#git fetch --all
#git pull --all
done
基本上它应该给我没有本地分支的项目;但是,当我执行此脚本时(分支 fake-news2 仅在远程本地不存在),我得到以下信息:
root@alfred:/etc/git-fetch# ./git-fetch.sh
updating handheldserver
working on origin/converttospringbootproject
5eee6f6d65feb2f49f4f7a4edecc37a17081dfae
working on origin/fake-news
95e840807a2c7d125c053a6a06bb1e9c7fd11b78
working on origin/fake-news2
95e840807a2c7d125c053a6a06bb1e9c7fd11b78
working on origin/fake-news3
497ecdc1e31f312884fe9666ef3408f3bd8083c8
working on origin/master
1a541521941687ae27bccfa412c4372db9afeaea
working on origin/maven-spring-boot-plugin
46e25767e41a639c6cc7760fe4a2249d01a00060
请注意,fake-news2 的哈希值与 fake-news 相同 - 就像对 fake-news2 的调用使用了 fake-news 标签(我希望你喜欢我使用的名称的讽刺意味虚拟分支!)。但是,在命令行上运行时,我得到以下信息:
root@alfred:/mnt/raid/git/handheldserver# git rev-parse --verify fake-news
95e840807a2c7d125c053a6a06bb1e9c7fd11b78
root@alfred:/mnt/raid/git/handheldserver# echo $?
0
root@alfred:/mnt/raid/git/handheldserver# git rev-parse --verify fake-news2
fatal: Needed a single revision
root@alfred:/mnt/raid/git/handheldserver# echo $?
128
这是你所期望的,因为 fake-news2 没有本地分支。看起来 git 在循环中做了一些奇怪的事情,或者我正在运行的命令在 echo 但不是在命令上获得了 ${b} 的正确变量。
我完全被困住了,所以我希望其他人一直在这里。请注意,我尝试了其他 git 命令来检查本地分支的存在,例如 show-ref,但它们似乎都做同样的事情。
提前感谢您的帮助
【问题讨论】: