EN
Git / Bash - pull all projects together
7 points
In this short article we are going to show, how to create Bash script that runs git pull
command on all repositories.
Quick solution (create pull-all.sh
script):
xxxxxxxxxx
1
2
3
paths=("/path/to/repository_1" "/path/to/repository_2" "/path/to/repository_n")
4
5
root=$(cd "$(dirname "${BASH_SOURCE[0]}")" &> /dev/null && pwd)
6
7
cd "${root}"
8
for item in ${paths[@]};
9
do
10
echo "----------------------------------------"
11
echo "${item}"
12
echo "----------------------------------------"
13
path="${root}/${item}"
14
if [ -d "${path}" ] && [ -d "${path}/.git" ];
15
then
16
cd "${path}"
17
if [ -n "$(git status --porcelain)" ];
18
then
19
echo "Some local changes detected !!!"
20
else
21
git pull --all
22
fi
23
else
24
echo "Indicated path does not indicate repository.";
25
fi
26
echo ""
27
done
28
29
read # waiting for Enter key
Script running:
xxxxxxxxxx
1
./pull-all.sh
Note: don't forget to add executable permissions:
chmod u+x pull-all.sh
.
Example output:
xxxxxxxxxx
1
----------------------------------------
2
/path/to/repository_1
3
----------------------------------------
4
Already up to date.
5
6
----------------------------------------
7
/path/to/repository_2
8
----------------------------------------
9
Some local changes detected !!!
10
11
----------------------------------------
12
/path/to/repository_3
13
----------------------------------------
14
Already up to date.
In this section you can see approach that scans sibling directories to find and pull git repositories.
pull-all.sh
script file:
xxxxxxxxxx
1
2
3
root=$(cd "$(dirname "${BASH_SOURCE[0]}")" &> /dev/null && pwd)
4
5
cd "${root}"
6
for item in *;
7
do
8
if [ -d "${item}" ] && [ -d "${item}/.git" ];
9
then
10
echo "----------------------------------------"
11
echo "${item}"
12
echo "----------------------------------------"
13
cd "${item}"
14
if [ -n "$(git status --porcelain)" ];
15
then
16
echo "Some local changes detected !!!"
17
else
18
git pull --all
19
fi
20
echo ""
21
cd ..
22
fi
23
done
24
25
echo "DONE!"