Skip to content

Instantly share code, notes, and snippets.

@kevin-lee
Last active January 12, 2017 17:08
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevin-lee/5de308944b1f09443ac9 to your computer and use it in GitHub Desktop.
Save kevin-lee/5de308944b1f09443ac9 to your computer and use it in GitHub Desktop.
Git pull from multiple remote git repositories
Copyright 2015 Lee, Seong Hyun (Kevin)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
gitpullall()
{
repos_to_ignore="origin"
should_ignore=-1
echo "running: git pull"
git pull
echo ""
for remote in `git remote`
do
echo "=================================="
echo "Remote: $remote"
echo "----------------------------------"
for ignore in $repos_to_ignore
do
[ "$remote" == "$ignore" ] && { should_ignore=1; break; } || :
done
if [ "$should_ignore" == "-1" ]
then
THIS_BRANCH_NAME=`git rev-parse --abbrev-ref HEAD`
git branch -r | grep -s "^[[:space:]]\+$remote/$THIS_BRANCH_NAME$" 2>&1 > /dev/null || git branch -r | grep -s "^[[:space:]]\+$remote/$THIS_BRANCH_NAME""[[:space:]]\+" 2>&1 > /dev/null || {
echo "No info of the remote repo named '$remote' is found."
echo "So $remote will be fetched."
echo "running: git fetch $remote"
git fetch "$remote"
echo ""
}
if git branch -r | grep -sw "$remote/$THIS_BRANCH_NAME" 2>&1 > /dev/null ; then
echo "running: git pull $remote $THIS_BRANCH_NAME"
git pull "$remote" "$THIS_BRANCH_NAME"
else
echo "$THIS_BRANCH_NAME branch does not exist on the remote called '$remote'."
echo "So $remote will not be pulled from $remote."
fi
else
echo "$remote is on the ignored list so ignore it."
echo "Ignored: $remote"
fi
should_ignore=-1
echo "=================================="
echo ""
done
}
gitpullall()
{
repos_to_ignore="origin"
should_ignore=-1
echo "running: git pull"
git pull
echo ""
for remote in `git remote`
do
echo "=================================="
echo "Remote: $remote"
echo "----------------------------------"
for ignore in $repos_to_ignore
do
[ "$remote" = "$ignore" ] && { should_ignore=1; break; } || :
done
if [ "$should_ignore" = "-1" ]
then
THIS_BRANCH_NAME=`git rev-parse --abbrev-ref HEAD`
{ git branch -r | grep -s "^[[:space:]]\+$remote/$THIS_BRANCH_NAME$" 2>&1 > /dev/null } || { git branch -r | grep -s "^[[:space:]]\+$remote/$THIS_BRANCH_NAME""[[:space:]]\+" 2>&1 > /dev/null } || {
echo "No info of the remote repo named '$remote' is found."
echo "So $remote will be fetched."
echo "running: git fetch $remote"
git fetch "$remote"
echo ""
}
if git branch -r | grep -sw "$remote/$THIS_BRANCH_NAME" 2>&1 > /dev/null ; then
echo "running: git pull $remote $THIS_BRANCH_NAME"
git pull "$remote" "$THIS_BRANCH_NAME"
else
echo "$THIS_BRANCH_NAME branch does not exist on the remote called '$remote'."
echo "So $remote will not be pulled from $remote."
fi
else
echo "$remote is on the ignored list so ignore it."
echo "Ignored: $remote"
fi
should_ignore=-1
echo "=================================="
echo ""
done
}
@kevin-lee
Copy link
Author

Author: Lee, Seong Hyun (Kevin)
Late updated: 2015-03-07

Add gitpullall() function to ~/.bashrc (or ~/.bash_aliases) for bash and ~/.zshrc for zsh.

e.g.) add the information of gihub, gitlab and bitbucket remote repositories to your local git repository.

git remote add github git@github.com:YOUR_USERNAME/YOUR_PROJECT.git
git remote add gitlab git@gitlab.com:YOUR_USERNAME/YOUR_PROJECT.git
git remote add bitbucket git@bitbucket.org:YOUR_USERNAME/your_project.git

then run

gitpullall

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