Eric Bellet
Eric Bellet

Reputation: 2045

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

Upvotes: 0

Views: 6776

Answers (2)

Austin Schaffer
Austin Schaffer

Reputation: 31

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

Upvotes: 3

padawin
padawin

Reputation: 4476

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'.

Upvotes: 1

Related Questions