Git:将工作分支合并到master的最快方法是什么?

时间:2012-02-11 02:28:35

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

假设我在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指向相同的提交。

1 个答案:

答案 0 :(得分:2)

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

# 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(安全,但很奇怪)或直接更新ref:git update-ref master feature_branch(不安全 - 不检查它是否是快进)但通常你也可以快速转换分支。

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