简体   繁体   English

如何创建一个bash脚本以使用git checkout克隆存储库?

[英]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. 我想创建一个bash脚本来克隆存储库并使用git checkout。 I'm using Windows. 我正在使用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. 我想使用分支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. $PATH是一个环境变量,用于确定在查找特定程序或可执行文件时要搜索的目录。

https://en.wikipedia.org/wiki/PATH_(variable) 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. 似乎在顶部设置PATH阻止了bash能够找到git二进制文件。 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. 正如@tkausl提到的那样,在错误的地方创建一个名为$PATH的变量,它会覆盖现有变量,从而使git可执行文件无法找到。 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'.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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