Git fetch & pull for all repos once

Gheorghe Madalina Eleonora
3 min readApr 5, 2021
Emergency Git commands

For someone who’s working with more than 12 repositories at once, it is complicated to maintain all of them updated. So far I used the console and broke my nails with fetches and pulls by going to every repository. But it’s time to change this bad habit and use the technology to work for me.

At the beginning I had no idea how to do shell scripts. First I followed their tutorials from there and I used some help from Iulian Albu (many thanks to him for this).

I started by testing in the console this script found on StackOverflow for pulling all the repos from one folder:

find . -type d -depth 1 -exec git --git-dir={}/.git --work-tree=$PWD/{} pull \;

Since the script was working fine, I thought about having it saved in one place and every time I need it just to call the whole script instead of copy-pasting it in the console from a note.

I created in VisualStudio Code a file named “git-fetch-pull” and added the script. By that, every time I needed to pull everything at once for the current branch from all repositories, I just had to write in the console, being in the folder which contains all the repositories, the following command:

sh git-fetch-pull

But, after some time, I needed also to move to another branches and I didn’t have the latest work on them, since they were not pulled. For that, I added both fetch and pull commands:

find . -type d -depth 1 -exec git --git-dir={}/.git --work-tree=$PWD/{} fetch --all \;
find . -type d -depth 1 -exec git --git-dir={}/.git --work-tree=$PWD/{} pull \;

I used it for some time and I found it hard to read because the fetch was starting first for all the repositories, and after that the pull. This means, if I wanted to follow a specific repository I had to scroll a little (or more). And I started to refactor by following some tutorials and comments around the internet, finding that I can use a loop instead a find to go through all the repositories. Since I wanted to have the information about fetching and pulling in the same place, I just added them both for every repository next to each other.

for i in */.git; do (
git fetch --all;
git pull);
done

Since I like everything to be fancy ✨, I added some styling as well:

for i in */.git; do ( 
echo “📂” $i; cd $i/..;
echo “🔄 Fetching”;
git fetch --all;
echo “🔽 Pulling”;
git pull;
echo ‘————————————————————————‘);
done

So everything goes like:

--

--

Gheorghe Madalina Eleonora

✨ Passionate photographer and FE Developer @Cognizant, former FE Developer at @Deloitte Digital and @IBM.