Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Update all git repositories under a base directory
#!/bin/bash
# store the current dir
CUR_DIR=$(pwd)
# Let the person running the script know what's going on.
echo "\n\033[1mPulling in latest changes for all repositories...\033[0m\n"
# Find all git repositories and update it to the master latest revision
for i in $(find . -name ".git" | cut -c 3-); do
echo "";
echo "\033[33m"+$i+"\033[0m";
# We have to go to the .git parent directory to call the pull command
cd "$i";
cd ..;
# finally pull
git pull origin master;
# lets get back to the CUR_DIR
cd $CUR_DIR
done
echo "\n\033[32mComplete!\033[0m\n"
@mndrix
Copy link

mndrix commented Apr 3, 2015

Thank you. I use a slight variation:

find . -name .git -type d \
| xargs -n1 -P4 -I% git --git-dir=% --work-tree=%/.. remote update -p

Using -type d prevents execution for ".git" files inside submodules. Using -P4 runs four fetches in parallel to speed things up a bit (and continues to make progress elsewhere if a single repository hangs). I prefer git remote update -p instead of git pull so that I can deal with merge conflicts later or rebase after I'm offline.

@iblech
Copy link

iblech commented Apr 21, 2015

You need to enclose $CUR_DIR in quotes, like cd "$CUR_DIR" if $CUR_DIR happens to contain special characters. Alternatively, you could also run the loop body in a subshell (using (...)).

@topmotiveret
Copy link

topmotiveret commented May 17, 2015

Thanks - h5!

@CodeSammich
Copy link

CodeSammich commented Oct 29, 2015

Awesome. This works just as intended ^^

@dhilsand
Copy link

dhilsand commented Dec 31, 2015

Thanks this is what I was looking for!

@bennycode
Copy link

bennycode commented May 30, 2016

Call stuff! Even runs on Windows 10. 😎

@dmhowcroft
Copy link

dmhowcroft commented Sep 21, 2016

What license is associated with this Gist? MIT? Apache? DWTFYW? :)

@nkh
Copy link

nkh commented Oct 27, 2016

dmhowcroft,

Legally anything released without a copyright notice is public domain, the notice contains a year range. If you release something with a year range then a few years later you make changes without updating the copyright notice, then those changes are public domain.

It was nice of you to ask the author :)

@MrMikeyagi
Copy link

MrMikeyagi commented Dec 3, 2016

I have some gits organized with my other gits that no longer have working github pages and so when the script gets to them it asks for a username and password. Is there any way to add to the script to make it so it just enters nothing for both the username and password? (or any other way to make it skip that hangup). I'd like to keep the related gits together for organizational purposes if possible. Thank you so much for your time and this awesome and SUPER USEFUL script :)

EDIT - Also I forgot to add that I put "read" (without the quotes) at the end so I can read the text log before it closes :)

@devCharles
Copy link

devCharles commented Jun 18, 2017

hey guys if you want i have another script here, it follows the same idea here but with many other options, i am a noob so if you have some feedback or want to contribute please let me know :D
https://github.com/devCharles/gitRepoUpdater

@JamesSwift
Copy link

JamesSwift commented Oct 28, 2017

If you want this to work with submodules too, I'd suggest using a slightly altered version of @mndrix's solution:

find . -name .git -type d | xargs -n1 -P4 -I% git --git-dir=% --work-tree=%/.. fetch --all --recurse-submodules

If you need to manually enter passwords for some repos, use this instead:

find . -name .git -type d | xargs -n1 -I% git --git-dir=% --work-tree=%/.. fetch --all --recurse-submodules

The --recurse-submodules options tells git to also fetch commits from any submodules' upstream. It means that if you run this script then go offline and checkout a new version of one of your projects which references a new submodule commit, when you run git submodule update it will be still able to find the new commit.

@agentbellnorm
Copy link

agentbellnorm commented Mar 19, 2018

Perfect, thanks!

@haythamdouaihy
Copy link

haythamdouaihy commented Apr 4, 2018

simple and does what is needed, thank you!

@djangofan
Copy link

djangofan commented Apr 20, 2018

#!/bin/bash
# run this from your workspace, which contains all your Git repos
CUR_DIR=$(pwd)
echo "\nUpdating remotes for all repositories...\n"
for i in $(find . -mindepth 1 -maxdepth 1 -type d); do
    printf "\nIn Folder: $i";
    cd "$i";
    printf "\n"
    THIS_REMOTES="$(git remote -v)"
    arr=($THIS_REMOTES)
    OLD_REMOTE="${arr[1]}";
    NEW_REMOTE="${OLD_REMOTE/git.old.net/git.new.org}"
    printf "New remote: $NEW_REMOTE"
    printf "\n"
    git remote set-url origin "$NEW_REMOTE"
    cd $CUR_DIR
done
printf "\nComplete!\n"

@lonely-porcupine
Copy link

lonely-porcupine commented Apr 30, 2018

Hey guys,

thought about handling cases when there are some changes made in files or any other situation where just git pull won't succeed?

@crowtherdr
Copy link

crowtherdr commented May 23, 2018

Very helpful! Thank you

@iancrowther
Copy link

iancrowther commented Sep 3, 2018

thanks

@Nadeeshyama
Copy link

Nadeeshyama commented Sep 11, 2018

Thank you, very helpful. 👍

@rigtorp
Copy link

rigtorp commented Sep 19, 2018

Simplest solution to fetch all remotes:

$ find . -name .git -type d -exec git --git-dir '{}' fetch --all ';'

@harisankar-krishna-swamy
Copy link

harisankar-krishna-swamy commented Oct 29, 2018

For all find commands -prune will help save time by not searching inside of .git folders. (find man page mentions this).

find ~/GIT-REPOSITORIES ( -exec test -d '{}'/.git ; ) -print -prune

Pipe from there or so
find ~/GIT-REPOSITORIES ( -exec test -d '{}'/.git ; ) -print -prune | xargs -n1 -I% git --git-dir=%/.git --work-tree=%/ pull --all

@shastrula
Copy link

shastrula commented Jul 30, 2019

This is the variation I use:
#To Pull

#!/usr/bin/env bash for repo in $(find . -name ".git" | cut -c 3- | sed 's/.git//g'); do current_branch=git -C ${repo} branch\|grep '*'\|cut -d' ' -f2;echo -e "Pulling repo: ${repo} \t on branch: ${current_branch}"; git -C $repo pull done

#To View

#!/usr/bin/env bash for repo in $(find . -name ".git" | cut -c 3- | sed 's/.git//g'); do current_branch=git -C ${repo} branch\|grep '*'\|cut -d' ' -f2;echo -e "repo: ${repo} \t on branch: ${current_branch}" done

@boyter
Copy link

boyter commented Oct 22, 2019

dmhowcroft,

Legally anything released without a copyright notice is public domain, the notice contains a year range. If you release something with a year range then a few years later you make changes without updating the copyright notice, then those changes are public domain.

It was nice of you to ask the author :)

Actually that's not 100% true but fairly close. Its not possible for Australians or Germans to release anything public domain as neither counties allow citizens to give away their rights. Its one of the reasons its so important to license things.

@joeytwiddle
Copy link

joeytwiddle commented Nov 13, 2019

@harisankar-krishna-swamy

find . -maxdepth 8 -name '.git' -prune -type d -printf '%h\n' | parallel --eta 'echo {} && git -C {} pull'

remove echo and --eta if not needed.

@lucasmodrich
Copy link

lucasmodrich commented Apr 29, 2020

Works perfectly. Thanks!

@valentin9
Copy link

valentin9 commented Jun 10, 2020

Thanks!

@jazz-it
Copy link

jazz-it commented Sep 7, 2020

find . -maxdepth 8 -name '.git' -prune -type d -printf '%h\n' | parallel --eta 'echo {} && git -C {} pull'

Very simple, yet effective and blazingly fast. Amazing! Thanks, @harisankar-krishna-2015

@douglas
Copy link
Author

douglas commented Oct 19, 2020

Heh! I had this need again after 9 years and was surprised by the amount of comments =)

Thanks for all suggestions - I will update the gist with improvements to run on macos and Linux 👍

@jensthomsenEV
Copy link

jensthomsenEV commented Mar 9, 2021

How will this work in visual studio code with a workspace setup? I would like to be able to do a pull-from in all the repositories in the workspace, followed by a syncronization, except if there is a merge conflict.

@NorkzYT
Copy link

NorkzYT commented Jun 15, 2022

How will this work in visual studio code with a workspace setup? I would like to be able to do a pull-from in all the repositories in the workspace, followed by a syncronization, except if there is a merge conflict.

Did you find a solution to this or an alternative?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment