在使用 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 --Vevb.com
- 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
- }
新聞熱點
疑難解答