Git pull, Merge, Push master branch or specific branch into multiple branches at once using shell script

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

  1. dev_branch(on Dev server) – [To git merge developers code to dev_branch and git update Dev server for developer testing)
  2. release_branch(on release server) – [ To git merge developers build into release_branch and git update release server for Tester to test release/build)
  3. 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))
  4. Production server – master_branch (To merge passed release that is release_branch to master_branch and git update on Production server for users)
  5. Any other branches for anything to maintain like next_release_branch etc.
  6. 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.

About the Author

Sandeep Agrawal
working @ Kalpavruksh Technologies as Software Engineer. I am a simple and straightforward person adaptable to any environment and work culture. Like to work in a creative and challenging environment using cutting edge technologies where I could constantly learn and successfully deliver solutions to problems.

1 Comment on "Git pull, Merge, Push master branch or specific branch into multiple branches at once using shell script"

  1. Thanks for sharing 🙂

Leave a comment

Your email address will not be published.


*


0 Shares
Tweet
Pin
Share
Share
Share
%d bloggers like this: