【问题标题】:Git: What is the fastest way to merge a working branch to master?Git:将工作分支合并到 master 的最快方法是什么?
【发布时间】:2012-03-03 11:38:14
【问题描述】:

假设我在master 分支并创建了一个新分支:

git checkout -b feature_branch

我开始在 feature_branch 上工作,并且在某个时候我想使用 rebase 将我的更改合并到 master。所以,我愿意:

# Get the latest code on master
git checkout master
git pull

# Rebase on master and push the most updated 'feature_branch'
git checkout feature_branch
git rebase master
git push

# Merge 'feature_branch' to 'master' and push the updated 'master'
git checkout master
git merge feature_branch
git push

# Back to work on 'feature_branch'
git checkout feature_branch

有没有办法减少步骤并达到同样的效果?

在流程结束时,我希望 masterorigin/masterfeature_branchorigin/feature_branch 指向同一个提交。

【问题讨论】:

    标签: git git-rebase git-merge git-push


    【解决方案1】:

    您可以删除几个命令。这也是一样的:

    # you have to have the branch checked out to pull, since pulling means merging
    # into master, and you need a work tree to merge in
    git checkout master
    git pull
    
    # no need to check out first; rebase does the right thing with two arguments
    git rebase master feature_branch
    
    git checkout master
    git merge feature_branch
    
    # git push by default pushes all branches that exist here and on the remote
    git push
    
    git checkout feature_branch
    

    严格来说,合并到 master 保证是一个快进(一个普通的合并),所以它实际上不需要工作树,但实际上并没有内置的方式跳过结帐。有解决方法,例如推入同一个存储库:git push . feature_branch:master(安全,但奇怪)或直接更新参考:git update-ref master feature_branch(不安全 - 不检查它是否是快进)但通常你最好快速切换分支。

    另外请注意,如果您不想重新定位功能分支,您可以跳过它,而不是重写 feature_branch,并最终在 master 中进行合并提交,而不是重新定位 feature_branch 和快进合并。

    【讨论】:

    • 不错的答案!样式的东西,但可能是“git pull .feature_branch”而不是“git merge feature_branch”。添加 git push --tags 也可能很酷
    • @AdrianCornish: git pull . feature_branch 将做与git merge feature_branch 完全相同的事情,只是更令人困惑。而且我在这里看不到任何表明需要处理标签的东西。
    • 同意 - 标签可能是一个额外的功能:-)。同意这两个命令是相同的,我建议它的原因是拉取请求工作流用于很多开源的东西 - 所以实际上你是在响应你写的拉取请求。纯粹是一种心态——你正在将作者 X 在 Repo Y 中所做的更改拉入你的分支 Z。
    猜你喜欢
    • 2011-08-01
    • 2016-04-30
    • 1970-01-01
    • 2013-01-14
    • 2016-09-20
    • 1970-01-01
    • 1970-01-01
    • 2011-04-14
    相关资源
    最近更新 更多