This post is useful to those who are maintaining multiple branches on multiple servers and merge production branch or any specific branch to all other branches once something gets updated to production branch or the specific branch.
For example:
If you have a multiple server with its respective branches and you are making master as your base branch to create other branches and whenever you are merging some code to master, you want all other branches to have latest code from master like
- dev_branch(on Dev server) – [To git merge developers code to dev_branch and git update Dev server for developer testing)
- release_branch(on release server) – [ To git merge developers build into release_branch and git update release server for Tester to test release/build)
- stage_branch(on Stage server) – [To merge passed release that is release_branch into stage_branch and git update on Stage server for tester and client to test on Stage server(pre-production))
- Production server – master_branch (To merge passed release that is release_branch to master_branch and git update on Production server for users)
- Any other branches for anything to maintain like next_release_branch etc.
- And once release or any code is moved to Production then you want to merge master_branch or master into all other branches to make all the branches up-to-date with Production code.
So If you have this kind of structure or similar structure where you are maintaining multiple branches then it would be difficult to manually check out to all branches, merge master code into each branch and push the branch.
So doing it using shell script can be easy.
Pull multiple branches:
cd project-update-folder;
for i in $(git branch | sed ‘s/^.//’);
do
if [ $i = ‘master’ ] || [ $i = ‘dev-branch’ ] || [ $i = ‘stage-branch’ ]
|| [ $i = ‘release-branch’ ] || [ $i = ‘master_branch’ ] || [ $i = ‘next_release_branch’ ];;
then
echo -e “n——————————–nSwitching to branch: $i”;
git checkout $i;
echo -e “Pulling branch: $i”;
git pull origin $i;
echo -e “Pulled branch: $in”;
fi
done
Merge one branch to multiple branches:(master branch to all other branches)
cd project-update-folder;
for i in $(git branch | sed ‘s/^.//’);
do
if [ $i = ‘dev-branch’ ] || [ $i = ‘stage-branch’ ]
|| [ $i = ‘release-branch’ ] || [ $i = ‘master-branch’ ] || [ $i = ‘next_release_branch’ ];
then
echo -e “n——————————–nSwitching to branch: $i”;
git checkout $i;
echo -e “Merging master into $i”;
git merge master $i;
echo -e “Merged master into $in”;
fi
done
Push all branches:
cd project-update-folder;
for i in $(git branch | sed ‘s/^.//’);
do
if [ $i = ‘dev-branch’ ] || [ $i = ‘stage-branch’ ]
|| [ $i = ‘release-branch ] || [ $i = ‘master-branch’ ] || [ $i = ‘next_release_branch’ ];
then
echo -e “n——————————–nSwitching to branch: $i”;
git checkout $i;
echo -e “Pulling branch: $i”;
git pull origin $i;
echo -e “Pulled branch: $i”;
echo -e “Pushing branch: $i”;
git push origin $i;
echo -e “nPushed branch: $in”;
fi
done
Hope it helps someone to some extend. Stay tuned for more automated scripts and also scripts for aws management.
Thanks for sharing 🙂