简体   繁体   中英

How to create a bash script for cloning repositories with git checkout?

I want to create a bash script to clone repositories and use git checkout. I'm using Windows.

#!/bin/bash

PATH="C:\Users\Projects"
echo "$PATH"

git clone https://mygitrepository.com $PATH
cd "$PATH\mygitrepository"
git checkout Development 

cd ..
git clone https://mygitrepository2.com $PATH
cd "$PATH\mygitrepository2"
git checkout Development 

I want to have all the repositories cloned using the branch Development. But I have the next error:

> $ ./Clone_Repositories.sh C:\Users\\Projects ./Clone_Repositories.sh:
> line 5: git: command not found ./Clone_Repositories.sh: line 7: cd:
> mygitrepository: No such file or directory ./Clone_Repositories.sh:
> line 9: git: command not found ./Clone_Repositories.sh: line 13: git:
> command not found ./Clone_Repositories.sh: line 15: git: command not
> found

You might want to change the variable name that you're using. $PATH is an environment variable and us used to determine which directories are searched when looking for a particular program or executable.

https://en.wikipedia.org/wiki/PATH_(variable)

It looks like setting PATH at the top is preventing bash from being able to find the git binary. Try this instead.

#!/bin/bash

MY_PATH="C:\Users\Projects"
echo "$MY_PATH"

cd "$MY_PATH"
git clone https://mygitrepository.com
cd "$MY_PATH\mygitrepository"
git checkout Development

cd "$MY_PATH"
git clone https://mygitrepository2.com
cd "$MY_PATH\mygitrepository2"
git checkout Development

As @tkausl mentioned, creating a variable named $PATH in bad, it overwrote the existing one, making the git executable unfindable. Rename the variable and it should work:

> cat /tmp/t.sh 
#!/bin/bash

PATH="C:\Users\Projects"
echo "$PATH"

git status

ghislain@linux (1): ~/home_conf (master *=) ✔
> /tmp/t.sh 
C:\Users\Projects
/tmp/t.sh: line 6: git: command not found

And here with a different variable:

ghislain@linux (1): ~/home_conf (master *=) ✖ (148)
> cat /tmp/t.sh 
#!/bin/bash

PROJECTS_PATH="C:\Users\Projects"
echo "$PROJECTS_PATH"

git status

ghislain@linux (1): ~/home_conf (master *=) ✔
> /tmp/t.sh 
C:\Users\Projects
On branch master
Your branch is up to date with 'origin/master'.

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