Languages
[Edit]
EN

Git / Bash - pull all projects together

7 points
Created by:
Emerson-V
333

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):

#!/bin/bash

paths=("/path/to/repository_1" "/path/to/repository_2" "/path/to/repository_n")

root=$(cd "$(dirname "${BASH_SOURCE[0]}")" &> /dev/null && pwd)

cd "${root}"
for item in ${paths[@]};
do
	echo "----------------------------------------"
	echo "${item}"
	echo "----------------------------------------"
	path="${root}/${item}"
	if [ -d "${path}" ] && [ -d "${path}/.git" ];
	then
		cd "${path}"
		if [ -n "$(git status --porcelain)" ];
		then
			echo "Some local changes detected !!!"
		else
			git pull --all
		fi
	else
		echo "Indicated path does not indicate repository.";
	fi
	echo ""
done

read # waiting for Enter key

Script running:

./pull-all.sh

Note: don't forget to add executable permissions: chmod u+x pull-all.sh.

Example output:

----------------------------------------
/path/to/repository_1
----------------------------------------
Already up to date.

----------------------------------------
/path/to/repository_2
----------------------------------------
Some local changes detected !!!

----------------------------------------
/path/to/repository_3
----------------------------------------
Already up to date.

 

Automatic repositories detection

In this section you can see approach that scans sibling directories to find and pull git repositories.

pull-all.sh script file:

#!/bin/bash

root=$(cd "$(dirname "${BASH_SOURCE[0]}")" &> /dev/null && pwd)

cd "${root}"
for item in *;
do
	if [ -d "${item}" ] && [ -d "${item}/.git" ];
	then
		echo "----------------------------------------"
		echo "${item}"
		echo "----------------------------------------"
		cd "${item}"
		if [ -n "$(git status --porcelain)" ];
		then
			echo "Some local changes detected !!!"
		else
			git pull --all
		fi
		echo ""
		cd ..
	fi
done

echo "DONE!"

 

See also

  1. Bash - get current script directory path 

Alternative titles

  1. Git / Bash - pull all repositories together
  2. Git / Bash - how to pull all projects together?
  3. Git / Bash - how to pull all repositories together?
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

Git

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join