禁止从git中的分支分支

时间:2018-08-27 22:59:56

标签: git

在我的工作流程中,我几乎从不希望从master分支分支。在启动一个新功能时,有几次我是偶然地这样做的。当我这样做时,合并时会破坏我的历史记录。

如果我已经在分支机构上,那么防止自己创建新分支机构的好方法是什么?我知道git branch的第二个参数

git checkout -b newbranch master

但是我不确定我是否可以重新训练自己以始终提供它。理想情况下,当我输入git checkout -b newbranch时,这将是默认行为,或者当我尝试从分支分支时发出警告。

2 个答案:

答案 0 :(得分:1)

您可以为git创建一个 bash别名,以检查这种情况并重写您的checkout命令以遵循约定。在下面的此示例中,我保留一个传入的父分支,但是当未给出时默认为master

Bash Alias

#!/bin/bash
cmd=$1
opt=$2
branch=$3
parent=$4
if [[ $cmd = "checkout" ]] && [[ $opt = "-b" ]]; then
  if [ -z "$parent" ]; then
    parent="master"
  fi
  /usr/bin/git checkout -b $branch $parent
else
  /usr/bin/git "$@"
fi

测试命令序列

chmod +x /path/to/git-wrapper.sh
alias git=/path/to/git-wrapper.sh
mkdir test
cd ./test
git init
echo "First line" >readme.md
git add readme.md
git commit -m "Initial commit"
git checkout -b test1
echo "Second line" >> readme.md
git commit -am "Second line"
git checkout -b test2
echo "Third line" >> readme.md
git commit -am "Third line"
git checkout master
git branch -a
git log
git merge test1
git merge test2

示例输出

Initialized empty Git repository in ...
[master (root-commit) 11bd292] Initial commit
 1 file changed, 1 insertion(+)
 create mode 100644 readme.md
Switched to a new branch 'test1'
[test1 4ace272] Second line
 1 file changed, 1 insertion(+)
Switched to a new branch 'test2'
[test2 54b7fff] Third line
 1 file changed, 1 insertion(+)
Switched to branch 'master'
* master
  test1
  test2
Updating 11bd292..4ace272
Fast-forward
 readme.md | 1 +
 1 file changed, 1 insertion(+)
Auto-merging readme.md
CONFLICT (content): Merge conflict in readme.md
Automatic merge failed; fix conflicts and then commit the result.

合并失败是我们的预期结果(如果别名未处于活动状态,则我们将从test1分支到test2,合并将被快速转发。请进行测试! >

答案 1 :(得分:0)

我有一个自定义的批处理/别名,可以切换到master(也许是pull master)等,然后创建一个新分支。

另一个选择是先在远程仓库中创建分支。我不知道您为远程回购使用的解决方案是什么,但是这样可以使过程更加自觉。

相关问题