How to use Gitflow versioning for multiple master/develop branches

Source: Internet
Author: User
Tags eval tagname

Companies are git as version control, some of the company's project groups are using Gitflow, but our group is not mandatory, but I had an accident last month, summed up the branch management problem, so began to force themselves to use Gitflow, the previous project is a master and a develop, Checkout a branch of yourself and then merge (you can look at A-successful-git-branching-model without understanding). The

Problem arises: The project has several main branches and development branches, such as Master_sina, Master_qq. Master_buzz, and Gitflow can only specify a master/develop, So you're going to go before you start a feature/hotfix. Git/config inside Modify [gitflow "branch"] related main branch and Development branch, so inconvenient. Look at the source, give Gitflow add material

add features

    When you open the Feature/hotfix branch, but you don't want it anymore ( Of course you can git branch-d xx directly, use git flow hotfix/feature Delete, automatically help you delete this branch, so that you create another branch (git flow only allows you to have one Hotfix/feature branch at a time)
    Do you want to use Gitflow to delete other existing branches? You don't need git branch-d, you can git flow hotfix/feature delete XX
    For example, I specified master as Master_sina at Init, and when I wanted to create M Aster_qq's hotfix, I just need to start with the name is ' Qq_ ' to start, if there are other needs you can directly add the corresponding content in the source code

Example Git-flow-hotfix I mainly mark the part I modified

The code is as follows Copy Code
Init () {


Require_git_repo


Require_gitflow_initialized


Gitflow_load_settings


version_prefix=$ (eval "echo ' git config--get gitflow.prefix.versiontag '")


prefix=$ (git config--get gitflow.prefix.hotfix)


}


# Add Help to the option description


Usage () {


echo "Usage:git flow hotfix [list] [-v]"


echo "Git flow hotfix start [-F] <version> [<base>]"


echo "Git flow hotfix finish [-FSUMPK] <version>"


echo "Git flow hotfix publish <version>"


echo "Git Flow hotfix Delete [branch]"


echo "Git flow hotfix track <version>"


}


Cmd_default () {


Cmd_list "$@"


}





Cmd_list () {


Define_boolean verbose false ' verbose (more) output ' V


Parse_args "$@"





Local hotfix_branches


Local Current_branch


Local Short_names


Hotfix_branches=$ (echo "$ (git_local_branches)" | grep "^ $PREFIX")


If [-Z "$hotfix _branches"]; Then


Warn "No hotfix branches exist."


Warn ""


Warn "You can start a new hotfix branch:"


Warn ""


Warn "git flow hotfix start <version> [<base>]"


Warn ""


Exit 0


Fi


current_branch=$ (git branch--no-color | grep ' ^* ' | grep-v ' no branch ' | sed ' s/^*//g ')


Short_names=$ (echo "$hotfix _branches" | sed "s ^ $PREFIX g")





# Determine column width


Local width=0


Local Branch


For branch in $short _names; Todo


Local len=${#branch}


width=$ (Max $width $len)


Done


width=$ (($width +3))





Local Branch


For branch in $short _names; Todo


Local fullname= $PREFIX $branch


Local base=$ (git merge-base "$fullname" "$MASTER _branch")


Local master_sha=$ (git rev-parse "$MASTER _branch")


Local branch_sha=$ (git rev-parse "$fullname")


If ["$fullname" = "$current _branch"]; Then


printf "*"


Else


printf ""


Fi


if flag verbose; Then


printf "%-${width}s" "$branch"


If ["$branch _sha" = "$master _sha"]; Then


printf "(No commits yet)"


Else


Local tagname=$ (git name-rev--tags--no-undefined--name-only "$base")


Local Nicename


If ["$tagname" "!="]; Then


Nicename= $tagname


Else


nicename=$ (git rev-parse--short "$base")


Fi


printf "(Based on $nicename)"


Fi


Else


printf "%s" "$branch"


Fi


Echo


Done


}





Cmd_help () {


Usage


Exit 0


}





Parse_args () {


# Parse Options


FLAGS "$@" | | Exit $?


Eval set--"${FLAGS_ARGV}"


# read arguments into global variables


Version=$1


Branch= $PREFIX $version


# Here's how I master/develop, I'm going to judge the prefix of the new branch,


# If Qq_ begins, it will create branches based on MASTER_QQ and DEVELOP_QQ. So you can add some methods to your needs here.


Test ' expr match ' $@ ' "qq_" '-ne 0 && master_branch= "$MASTER _branch" _qq &&


Develop_branch= "$DEVELOP _branch" _qq


}





Require_version_arg () {


If ["$VERSION" = ""]; Then


Warn "Missing argument <version>"


Usage


Exit 1


Fi


}





Require_base_is_on_master () {


if! Git branch--no-color--contains "$BASE" 2>/dev/null


| Sed ' s/[*]//g '


| Grep-q "^ $MASTER _branch$"; Then


Die "Fatal:given base ' $BASE ' is not a valid commit on ' $MASTER _branch '."


Fi


}





Require_no_existing_hotfix_branches () {


Local hotfix_branches=$ (echo "$ (git_local_branches)" | grep "^ $PREFIX")


Local first_branch=$ (Echo ${hotfix_branches} | head-n1)


first_branch=${first_branch# $PREFIX}


[Z "$hotfix _branches"] | |


Die "There is a existing hotfix branch ($first _branch). Finish that one.


}


# Add Delete parameter, function requires Cmd_ start


Cmd_delete () {


If ["$" = "]; Then


# When you don't specify a parameter, automatically find an Gitflow branch that is not closed


Local hotfix_branches=$ (echo "$ (git_local_branches)" | grep "^ $PREFIX")


Test "$hotfix _branches" = "&& die" There has not existing hotfix branch can delete && exit 1


Else


# Specify the parameters to determine the number format of the parameter first


Test $#!= 1 && die "There only need one parameter indicates the branch to be deleted" && exit 1


Hotfix_branches= "$"


Fi


# when the branch to be deleted is the current branch, first checkout to the develop branch


Test "$hotfix _branches" = "$ (git_current_branch)" && Echo ' checkout Develp branch '; Git_do Checkout "$DEVELOP _branch"


git branch-d ${hotfix_branches} >/dev/null 2>&1&& echo ' Delete successed ' | | Die "Did not find branch: [$hotfix _branches]"





}


Cmd_start () {


Define_boolean fetch false "fetch from $ORIGIN before performing finish" F


Parse_args "$@"


base=${2:-$MASTER _branch}


Require_version_arg


Require_base_is_on_master


Require_no_existing_hotfix_branches





# Sanity Checks


Require_clean_working_tree


Require_branch_absent "$BRANCH"


Require_tag_absent "$VERSION _prefix$version"


If flag fetch; Then


Git_do fetch-q "$ORIGIN" "$MASTER _branch"


Fi


If has "$ORIGIN/$MASTER _branch" $ (git_remote_branches); Then


Require_branches_equal "$MASTER _branch" "$ORIGIN/$MASTER _branch"


Fi





# Create Branch


Git_do checkout-b "$BRANCH" "$BASE"





Echo


echo "Summary of Actions:"


echo "-A new branch ' $BRANCH ' is created, based on ' $BASE '"


echo "-You are now on branch ' $BRANCH '"


Echo


echo "Follow-up actions:"


echo "-Bump the version number now!"


echo "-Start committing your hot fixes"


echo "-When done, run:"


Echo


echo "Git flow hotfix finish ' $VERSION '"


Echo


}





Cmd_publish () {


Parse_args "$@"


Require_version_arg





# Sanity Checks


Require_clean_working_tree


Require_branch "$BRANCH"


Git_do fetch-q "$ORIGIN"


Require_branch_absent "$ORIGIN/$BRANCH"





# Create Remote Branch


GIT_DO push "$ORIGIN" "$BRANCH: refs/heads/$BRANCH"


Git_do fetch-q "$ORIGIN"





# Configure remote Tracking


git config "branch. $BRANCH. Remote" "$ORIGIN"


git config "branch. $BRANCH. Merge" "Refs/heads/$BRANCH"


Git_do Checkout "$BRANCH"





Echo


echo "Summary of Actions:"


echo "-A new remote branch ' $BRANCH ' was created '


echo "-The local branch ' $BRANCH ' is configured to track the remote branch"


echo "-You are now on branch ' $BRANCH '"


Echo


}





Cmd_track () {


Parse_args "$@"


Require_version_arg





# Sanity Checks


Require_clean_working_tree


Require_branch_absent "$BRANCH"


Git_do fetch-q "$ORIGIN"


Require_branch "$ORIGIN/$BRANCH"





# Create Tracking Branch


Git_do checkout-b "$BRANCH" "$ORIGIN/$BRANCH"





Echo


echo "Summary of Actions:"


echo "-A new Remote Tracking branch ' $BRANCH ' was created '


echo "-You are now on branch ' $BRANCH '"


Echo


}





Cmd_finish () {


Define_boolean fetch false "fetch from $ORIGIN before performing finish" F


Define_boolean sign False "sign the release tag cryptographically" s


Define_string signingkey "" "Use the given Gpg-key for the digital signature (implies-s)" U


Define_string message "", use the given tag message m


Define_string messagefile "" "Use the contents of the given file as tag message" F


Define_boolean push false "push to $ORIGIN after performing finish" p


Define_boolean Keep false "keep branch after performing finish" K


Define_boolean Notag false "don ' t tag this release" N


Parse_args "$@"


Require_version_arg





# Handle flags that imply the other flags


If ["$FLAGS _signingkey" "!="]; Then


flags_sign= $FLAGS _true


Fi





# Sanity Checks


Require_branch "$BRANCH"


Require_clean_working_tree


If flag fetch; Then


Git_do fetch-q "$ORIGIN" "$MASTER _branch" | |


Die "could not fetch $MASTER the _branch from $ORIGIN."


Git_do fetch-q "$ORIGIN" "$DEVELOP _branch" | |


Die "could not fetch $DEVELOP the _branch from $ORIGIN."


Fi


If has "$ORIGIN/$MASTER _branch" $ (git_remote_branches); Then


Require_branches_equal "$MASTER _branch" "$ORIGIN/$MASTER _branch"


Fi


If has "$ORIGIN/$DEVELOP _branch" $ (git_remote_branches); Then


Require_branches_equal "$DEVELOP _branch" "$ORIGIN/$DEVELOP _branch"


Fi





# try to merge into master


# in case a previous attempt to finish this release branch has failed,


# but the merge into Master is successful, we skip it now


if! Git_is_branch_merged_into "$BRANCH" "$MASTER _branch"; Then


Git_do Checkout "$MASTER _branch" | |


Die "Could not check out $MASTER _branch."


Git_do merge--no-ff "$BRANCH" | |


Die "There were merge conflicts."


# Todo:what do we?


Fi





If Noflag Notag; Then


# try to tag the release


# in case a previous attempt to finish this release branch has failed,


# But the tag is set successful, we skip it now


Local tagname= $VERSION _prefix$version


if! Git_tag_exists "$tagname"; Then


Local opts= "-a"


Flag sign && opts= "$opts-S"


["$FLAGS _signingkey"!= "] && opts=" $opts-u ' $FLAGS _signingkey ' "


["$FLAGS _message"!= "] && opts=" $opts-M ' $FLAGS _message ' "


["$FLAGS _messagefile"!= "] && opts=" $opts-F ' $FLAGS _messagefile ' "


Eval git_do tag $opts "$VERSION _prefix$version" "$BRANCH" | |


Die "Tagging failed. Please run the finish again to retry.


Fi


Fi





# try to merge into develop


# in case a previous attempt to finish this release branch has failed,


# but the merge into develop is successful, we skip it now


if! Git_is_branch_merged_into "$BRANCH" "$DEVELOP _branch"; Then


Git_do Checkout "$DEVELOP _branch" | |


Die "Could not check out $DEVELOP _branch."





# todo:actually, accounting for ' git describe ' pays, so we should


# Ideally git merge--no-ff $tagname here, instead!


Git_do merge--no-ff "$BRANCH" | |


Die "There were merge conflicts."


# Todo:what do we?


Fi





# Delete Branch


if Noflag keep; Then


# This question is very strange, in completing the branch delete it will also exist the current branch is


# to delete the branch to delete the error, so first switch away


Test "$BRANCH" = "$ (git_current_branch)" && git_do Checkout "$DEVELOP _branch"


Git_do branch-d "$BRANCH"


Fi





If flag push; Then


GIT_DO push "$ORIGIN" "$DEVELOP _branch" | |


Die "could not pushed to $DEVELOP the _branch from $ORIGIN."


GIT_DO push "$ORIGIN" "$MASTER _branch" | |


Die "could not pushed to $MASTER the _branch from $ORIGIN."


If Noflag Notag; Then


GIT_DO push--tags "$ORIGIN" | |


Die "could not push tags to $ORIGIN."


Fi


Fi





Echo


echo "Summary of Actions:"


echo "-Latest objects have been fetched from ' $ORIGIN '"


echo "-Hotfix branch has been merged into ' $MASTER _branch '"


If Noflag Notag; Then


Echo "-The hotfix was tagged ' $VERSION _prefix$version '"


Fi


echo "-Hotfix branch has been back-merged into ' $DEVELOP _branch '"


if flag keep; Then


echo "-Hotfix branch ' $BRANCH ' is still available"


Else


echo "-Hotfix branch ' $BRANCH ' has been deleted"


Fi


If flag push; Then


echo "-' $DEVELOP _branch ', ' $MASTER _branch ' and tags have been pushed to ' $ORIGIN '"


Fi


Echo


}





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.