假设我在 master
分支上创建了一个新分支:
git checkout -b feature_branch
我开始处理 feature_branch
,在某些时候我想使用 rebase 将我的更改 merge 到 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
有没有办法减少步骤数并达到同样的效果?
在流程结束时,我想要 master
、origin/master
、feature_branch
和 origin/feature_branch
,指向相同的提交。
最佳答案
您可以删除几个命令。这也是一样的:
# 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
严格来说, merge 到 master 保证是快进的(简单的 merge ),所以它实际上不需要工作树,但实际上并没有内置的方法来跳过 checkout 。有解决方法,例如推送到同一个存储库: git push 。 feature_branch:master
(安全,但很奇怪)或直接更新 ref:git update-ref master feature_branch
(不安全 - 不检查它是否快进)但通常你可能以及快速切换分支。
另请注意,如果您不想对功能分支进行 rebase ,您可以跳过它,而不是重写 feature_branch,并最终在 master 中进行 merge 提交,而不是 rebase feature_branch 和快进 merge 。
关于Git:将工作分支 merge 到 master 的最快方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9237311/