ado ado - 7 months ago 20
Bash Question

Make a shell script to update 3 git repos

I am working with 5 repos that I have cloned in my development environment.
When I want to update a git repo, I enter the folder /home/adrian/repo1/ and do:

git checkout master
git pull origin master

But then, every morning I have to do the same thing for the other 4 repos. This is quite troublesome.

Can I put this in a shell script? I mean, if I write these git commands in the shell script, and run it, will I be able to update all the repos?

I was thinking of writing something like this...

cd repo1
git checkout master
git pull origin master
cd ..
cd repo2
git checkout master
git pull origin master
cd ..


(i'm on linux)

Edit: Maybe this is more challenging than what I thought. Most times when I do "git pull origin master", i get erorrs like "Your local changes to .... would be overwritten by merge." So i have to enter into the respective branch and stash the stuff..

Edit 2:

What I'm thinking of doing is, if a conflict happens, ignore it and go to the next repo

cd repo1
git checkout master
git pull origin master

(if there is conflict, ignore and go to the next line but dont stop here)

cd ..
cd repo2
git checkout master
git pull origin master
cd ..


but i dont know how to write the thing in parenthesis.

Answer

First, I recommend against using git pull. Instead, create a safer git up alias:

git config --global alias.up '!git remote update -p; git merge --ff-only @{u}'

See this answer for an explanation of git up.

Then you can safely script it:

#!/bin/sh
for repo in repo1 repo2 repo3 repo4; do
    (cd "${repo}" && git checkout master && git up)
done