简体   繁体   中英

Git/bash Updating local branches

I'm writing a small bash script as below:

#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

Basically it should give me the projects that do nto have a local branch; however when I execute this script (the branch fake-news2 doesn't exist locally only remotely) I get the following:

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

Note that the hash for fake-news2 is the same as fake-news - it is like the call to fake-news2 is using the fake-news tag (I hope you like the irony of the names I'm using for dummy branches!). However when run on the command line I get the following:

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

This is what you expect as fake-news2 does not have a local branch. It looks like either git is doing something weird in the loop or the command I am running gets the right variable for ${b} on the echo but not on the command.

I'm totally stuck so I'm hoping someone else has been here. Note that I've tried other git commands to check for the existing of a local branch such as show-ref but they all seem to do the same thing.

Thanks, in advance for your help

Why are you expecting those two command have the same result, if fake-news2 doesn't exist locally?

git rev-parse --verify fake-news2 #The one you used on the command-line
git rev-parse --verify origin/fake-news2 #The one you used in your script

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM