多master/develop分支如何使用gitflow版本控制

 在使用 gitflow 做版本控制系统,发现gitflow的时候只能指定一个master/develop,如果要多分支使用要如何操作呢?那么来看看我是如何给gitflow加料的。

 
 

公司都是git作为版本控制,公司一些项目组在用gitflow,但是我们组没有强制, 但是我上月出了一次事故,总结就是分支管理问题,所以开始强迫自己使用gitflow, 以前的项目是一个master和一个develop,自己checkout一个分支,然后merge(不理解的可以看看a-successful-git-branching-model).

问题出现了: 项目有几个主分支和开发分支,比如master_sina, master_qq. master_buzz ,而gitflow的时候只能指定一个master/develop, 这样你start一个feature/hotfix之前就要去.git/config里面修改 [gitflow “branch”]项的相关主分支和开发分支,so不方便。看了下源码,给gitflow加点料

添加功能

    当你打开了feature/hotfix分支,但是你不想要它了(当然你可以直接git branch -D xx),使用git flow hotfix/feature delete ,自动帮你删除这个分支,以便你新建其他分支(git flow只容许你一次存在一个hotfix/feature分支)
    你想使用gitflow删除其它存在分支嘛?不需要 git branch -D ,你还可以git flow hotfix/feature delete XX
    比如我在init的时候指定了master为master_sina, 而当我想创建master_qq的hotfix,我只需要在start的是否给它取名字是’qq_‘开头的即可,要是有其它的需要你可以直接在源码里面添加对应的内容

例子 git-flow-hotfix 我主要标记我修改的部分

 代码如下 复制代码
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)
}
# 增加help的选项说明
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 first
    local width=0
    local branch
    for branch in $short_names; do
        local len=${#branch}
        width=$(max $width $len)
    done
    width=$(($width+3))

    local branch
    for branch in $short_names; do
        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
    # 这里就是我多master/develop的技巧,我这里会判断要新建的分支的前缀,
    # 要是qq_开头就会基于master_qq和develop_qq创建分支。所以你可以根据你的需要在这里加一些方法
    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 an existing hotfix branch ($first_branch). Finish that one first."
}
# 添加delete 参数,函数需要cmd_开头
cmd_delete() {
    if [ "$1" = "" ]; then
        # 当不指定参数自动去找存在的未关闭的gitflow分支
        local hotfix_branches=$(echo "$(git_local_branches)" | grep "^$PREFIX")
        test "$hotfix_branches" = "" && die "There has not existing hotfix branch can delete" && exit 1
    else
        # 指定参数先判断参数是不是的数量格式
        test $# != 1 && die "There only need one parameter indicates the branch to be deleted" && exit 1
        hotfix_branches="$1"
    fi
    # 当要删除的分支就是当前分支,先checkout到develop分支
    test "$hotfix_branches" = "$(git_current_branch)" && echo 'First 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' was 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' was 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 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_BRANCH from $ORIGIN."
        git_do fetch -q "$ORIGIN" "$DEVELOP_BRANCH" || 
          die "Could not fetch $DEVELOP_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 was 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 do now?
    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 was 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 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 was 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 do now?
    fi

    # delete branch
    if noflag keep; then
        # 这个问题很奇怪,在完成分支删除它也会存在当前分支是
        # 要删除的分支删除报错的问题,所以先切换走
        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 push to $DEVELOP_BRANCH from $ORIGIN."
        git_do push "$ORIGIN" "$MASTER_BRANCH" || 
            die "Could not push to $MASTER_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
    }

时间: 2024-07-28 18:42:00

多master/develop分支如何使用gitflow版本控制的相关文章

分布式版本控制Git分支管理策略及使用规范流程

Git分支管理策略 目前最流行的"版本管理系统",非Git莫属. 相比同类软件,Git有很多优点.其中很显著的一点,就是版本的分支(branch)和合并(merge)十分方便.有些传统的版本管理软件,分支操作实际上会生成一份现有代码的物理拷贝,而Git只生成一个指向当前版本(又称"快照")的指针,因此非常快捷易用. 但是,太方便了也会产生副作用.如果你不加注意,很可能会留下一个枝节蔓生.四处开放的版本库,到处都是分支,完全看不出主干发展的脉络. Vincent Dr

Git分支本地操作详解

引言 在上一节中我们对Git的常用本地操作的命令进行详解,而本节要讲解的是Git的分支, 在讲解之前补充两点概念性的东西: 第一个: 第一节中一个读者提出的疑问,Git和SVN在版本控制中存储方式版本信息的差异. 答:Git关心文件的整体是否发生变化,而SVN则关心的是文件内容的具体差异! SVN每次记录的是有哪些文件进行了修改,以及修改了哪些行的哪些内容: 如上图,比如版本2中记录的是文件A以及文件C的变化,而版本3中仅仅记录文件C 的变化这样,以此类推:而Git并不保存这些前后变化的差异数据

git学习------&amp;gt;Git 分支管理最佳实践

ps:本文转载于 : https://www.ibm.com/developerworks/cn/java/j-lo-git-mange/index.html Git 是目前最流行的源代码管理工具.大量的软件项目由 GitHub.Bitbucket 和 GitLab 这样的云服务平台或是私有的 Git 仓库来管理.在使用 Git 时通常会遇到的一个问题是采用何种分支管理实践,即如何管理仓库中作用不同的各类分支.和软件开发中的其他实践一样,Git 分支管理并没有普遍适用的最佳做法,而只有对每个团队

Git详解之三:Git分支

原文链接:http://blog.jobbole.com/25877/ 原文:<Pro Git> Git 分支 几乎每一种版本控制系统都以某种形式支持分支.使用分支意味着你可以从开发主线上分离开来,然后在不影响主线的同时继续工作.在很多版本控制系统中,这是个昂贵的过程,常常需要创建一个源代码目录的完整副本,对大型项目来说会花费很长时间.(伯乐在线注:如果你对Git还不了解,建议从本Git系列第一篇文章开始阅读) 有人把 Git 的分支模型称为"必杀技特性",而正是因为它,将

一个成功的Git分支模型

本文中我会展示一种开发模型,一年前该模型就已经被我用在所有的项目中(包括工作中的项目和私有项目),结果是非常成功的.我早就想为此写点东西,可直到现在才有时间.本文不会讲述任何项目的细节,只会涉及到分支策略和发布管理. git-branch-1 本文使用Git作为所有源码的版本控制工具. 为什么是Git? 要全面了解Git与其它集中式版本控制系统相比的优劣,可以参考这个页面.这方面的争论可谓是硝烟弥漫.作为一个开发者,所有这些工具中我最钟情于Git.Git的的确确改变了人们考虑合并及分支的方式.在

项目管理gitflow的用法(转)

在这里主要讲一下我在项目中用到的关于gitflow的用法.   公司的项目中,专门有一台用来存放版本库的服务器,路径是在默认的安装目录/opt/git/,那么在使用的时候,如果你是一个功能模块或者是一个项目的发起者的话,那么你可以选择使用gitflow来进行管理,关于gitflow的好处在这里我就不在多讲了.   1.首先在上面刚刚讲到的目录(注意这是在专门的盛放版本库的那台服务器上)内,创建一个目录,比如说叫testflow.git.   2.cd testflow.git  ---->  g

git对远程分支和tag的操作

技术 Git查看.删除.重命名远程分支和tag 11/17/2012zrong7条评论69,235 次查看 本站文章除注明转载外,均为本站原创或者翻译. 本站文章欢迎各种形式的转载,但请18岁以上的转载者注明文章出处,尊重我的劳动,也尊重你的智商: 本站部分原创和翻译文章提供markdown格式源码,欢迎使用文章源码进行转载: 本文标题:Git查看.删除.重命名远程分支和tag 本文链接:http://zengrong.net/post/1746.htm Git查看.删除.重命名远程分支和tag

Git 切换分支和merge分支

参考: Git 提交代码流程: http://hw1287789687.iteye.com/blog/2221759   Git 切换分支流程: 步骤一不能少,不然很可能发生develop分支中修改的内容被覆盖的悲惨事故    Git 分支合并流程 案例:把develop 合并到master分支      

玩转git分支

搞个代码的管理工具,居然不弄上分支啥的东西.这简直太low了.尤其是在使用了传说中得很牛X的Git的时候,尤其显得low.拿着青龙偃月刀当烧火棍子使,关公知道了还不重反人间教育你!? 远程分支 要说分支就一定要从分支产生的最遥远的历史谈起.这一切开始于你用clone命令从远端把代码库的代码拉取到本地开始.这个时候,git自动把这个远端代码库命名为origin并自动创建一个origin/master分支.相对的在本地创建一个叫做master的本地分支.这个时候这两个分支的指针都是指向一个地方的(不