DEV Community

Cover image for Git Repo Sync: Simple Bash script to sync master and develop branches for a classic Git Flow Branching Model
Aniello Musella
Aniello Musella

Posted on • Updated on

Git Repo Sync: Simple Bash script to sync master and develop branches for a classic Git Flow Branching Model

I wanna share an useful bash script (at least according to me ;)) that I wrote to make easier the synchronization of one or more repositories in a git flow branching model that many of you know and use daily to version your code base (if you don't know what's a git flow branching model please refer this link).

This script has the goal to synchronize the branches develop and master from local to remote repository.

To know how to run the script type :

animus@host:~$./repo.sync --help
Enter fullscreen mode Exit fullscreen mode

and you get the usage of the script and all of possible options.

usage: repo.sync [OPTION]... DIRECTORY... 
Sync a Git repository for a DIRECTORY (one or more) representing a git repository where is activated a Git Flow branching model.

Options:
--noPushOnProdBranch        Skip git push on production branch
--noPushTagsOnProdBranch    Skip git push tags on production branch
--noPullOnProdBranch        Skip git pull of production branch from remote.
--noPushOnDev               Skip git push on develop branch
--noPushTagsOnDev           Skip git push tags on develop branch
--noPushTagsOnDev           Skip git pull of develop branch from remote.
--mergeProdInDev            Make a merge from production branch into develop branch (Warning: this breaks git-flow branching model).
--help                      Display this help and exit
Enter fullscreen mode Exit fullscreen mode

You can find the script on my gist on github.

Suggestions, corrections and improvements are welcome!

See you soon ;)

Top comments (8)

Collapse
 
joelbonetr profile image
JoelBonetR 🥇

Doesn't it break the gitflow itself?

I mean, you code on a feature/branch, then integrate on Develop and merge to Master. At this point you already have Develop and Master sync-ed

Collapse
 
animusna profile image
Aniello Musella

No, it doesn't. The goal of the script is to maintain sync the local master and develop branches with remote repository automating sync commands.

For instance you can run the script before each git flow start command like a new feature

animus@host:~$repo.sync .
animus@host:~$git flow feature start MYFEATURE

and before and after of each git flow finish command

animus@host:~$repo.sync .
animus@host:~$git flow feature finish MYFEATURE
animus@host:~$repo.sync .

In this way you are sure to start and finish with an updated develop branch and to sync your main local branches (develop and master) with remote repo.

See you.

Collapse
 
joelbonetr profile image
JoelBonetR 🥇 • Edited

I keep misunderstanding what issue this solves.
If you mean that this script only sync local develop with remote develop, and local master with remote master, i assume it's the same than performing checkout (develop or master) and pull. Nothing else.

Then you need to checkout again your working branch manually

Am I wrong?

Thread Thread
 
animusna profile image
Aniello Musella

You are not wrong the script run checkout and pull of master and develop, but sometimes the sync could be bidirectional so you have to push your updated branches to the remote repository executing other commands.

For instance when you finish a release you have to run at least three commands to update the remote repository (develop and master):

animus@host:~$git push origin master
animus@host:~$git push origin develop
animus@host:~$git push origin --tags

The script permit you to stay synchronized running one command instead of running the same number of git commands for each branch (develop and master).

The issue solved is to not repeat the same commands for each branch especially when you update very frequently your code base.

Thread Thread
 
joelbonetr profile image
JoelBonetR 🥇

Nice, I see the use case now.
I've been using git console commands for a while but finally I moved to GitKraken which adds useful extra features (no more pain on Merges, GBoards, issues sync with github, gitlab...), you must take a try :)

Thread Thread
 
animusna profile image
Aniello Musella

GitKraken it's cool, thanks for the invitation! :)

I'll try it, even though I'm little bit console addicted ;)

Thread Thread
 
joelbonetr profile image
JoelBonetR 🥇

I need to waste my time coding instead on dealing with the VCS, specially when there's a merge that needs attention because auto-merge failed, so it's a nice tool for that as you can pick chunks of code from a file or another in the file compare window when merging.
I'm also loving the ability to sync with some Git servers, specially the issues tracker, hope it helps to you :D

Thread Thread
 
animusna profile image
Aniello Musella

I understand, productivity it's important and for this reason sometimes I also use gui tools. What I use it's up to scenario I'm facing.

See you ;)