Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/bin/bash
# Fetch the latest branches
git fetch
# Get the repository path
repoPath=$(git config --get remote.origin.url)
dir=$(pwd)
# Loop through each branch
for branches in $(git for-each-ref --format='%(refname)' refs/remotes/)
do
# Remove redundant string from branch name
myBranch=${branches/refs\/remotes\/origin\//}
# Check if we are using 'HEAD' branch
if [ "$myBranch" == "HEAD" ]
then
continue
fi
# Check if the branch has already been checked out
if [ -e "$dir"/git/branches/"$myBranch" ]
then
# Switch to the branch directoryls
cd "$dir"/git/branches/"$myBranch" || exit
echo "$myBranch" ' - Checking for updates.'
# Pull latest code
status=$(git pull)
# Check if there are any changes
if [ "$status" == "Already up-to-date." ]
then
echo "$myBranch" ' - Already up-to-date.'
continue
else
echo "$myBranch" ' - Updating node packages'
npm install
continue
fi
else
# Switch to branches directory
cd "$dir"/git/branches/ || exit
# Clone repository to branch named directory
git clone "$repoPath" "$myBranch"
# Switch to specific branch directory
cd "$dir"/git/branches/"$myBranch" || exit
npm install
# Switch to the branch
git checkout "$myBranch"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment