Last active
March 28, 2017 14:23
-
-
Save Tanapruk/f35eb0685da1f57e8cc110e3b7893a24 to your computer and use it in GitHub Desktop.
Looping through all local git branch and git pull them each
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function gitPullAllLocal() { | |
#if there is a passing argument | |
if [ -z $1 ] | |
then | |
GIT_RELATIVE_DIRECTORY="$(pwd)" | |
else | |
GIT_RELATIVE_DIRECTORY=$1 | |
fi | |
#go to that relative path if exists | |
cd ${GIT_RELATIVE_DIRECTORY} | |
CURRENT_BRANCH="$(getCurrentBranch)" | |
#Checkout develop if detached head | |
if [ ${CURRENT_BRANCH} = "(HEAD" ] | |
then | |
echo 'found detached HEAD gonna checkout develop' | |
git checkout develop | |
CURRENT_BRANCH="$(getCurrentBranch)" | |
fi | |
ALL_BRANCH_LIST="$(git branch)" #keep list to a variable | |
ALL_BRANCH_LIST="${ALL_BRANCH_LIST//\*/ }" #replace * character. Syntax is ${string//substring/replacement} | |
echo "======================================" | |
echo "Path: ${GIT_RELATIVE_DIRECTORY}" | |
echo "gonna loop through" | |
echo "${ALL_BRANCH_LIST}" | |
echo "======================================" | |
#loop through each local branch, checkout and pull | |
for branch in ${ALL_BRANCH_LIST} | |
do | |
git checkout ${branch} | |
git pull origin ${branch} | |
done | |
#Revert to current branch | |
git checkout ${CURRENT_BRANCH} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment