英文:
how to git pull automatically when working on same branch
问题
在git flow中,有master
,dev
,feature/*
分支。
开发者在开始工作时应该从dev
切换到feature/*
分支。
但我发现,如果有人先推送到dev
分支,另一个人在正确推送之前应该先拉取dev
。
所以,步骤如下:
- 开发功能并提交。
- 切换到
dev
分支并合并feature/*
。 - git推送,由于其他人先推送而被拒绝。
为了防止这种情况,应该在合并之前执行git pull origin dev
。是否有自动化这一步骤的方法?
英文:
in git flow, you have master
, dev
, feature/*
branches.
developers should checkout from dev
to feature/*
branch when they start working on something.
But I've found that when someone else pushes to dev
branch first, another person should pull dev
first in order to push properly.
So, it goes like this.
- work on feature and commit.
- checkout to
dev
branch and mergefeature/*
. - git push, and gets rejected because someone else pushed first.
To prevent this, one should git pull origin dev
before merging. Is there any way to automate this step?
答案1
得分: 1
最佳做法是首先将您的功能分支rebase到目标分支(这里是dev)
# 使用最新内容更新dev
git switch dev
git pull
# 将myFeature分支rebase到dev分支之上
git switch myFeature
git rebase dev
# 然后合并将变得简单
git switch dev
git merge myFeature
git push
英文:
The best practice is to rebase first your feature branch on top of the target branch (here dev)
# update dev with the latest
git switch dev
git pull
# rebase myFeature on top of dev
git switch myFeature
git rebase dev
# then the merge is trivial
git switch dev
git merge myFeature
git push
答案2
得分: 0
这不是直接回答你的问题,但这是我的看法:
在我所在的团队中,我们从不直接推送到开发分支。相反,我们会从特性分支创建一个拉取请求到开发分支。我们还要求在接受拉取请求之前,特性分支必须合并最新版本的开发分支。
在一些项目中,我们采取了进一步的措施,消除了开发分支。我们只有一个main
分支。特性分支是从main
创建的,然后将拉取请求提交回main
分支。
英文:
This doesn't directly answer your question, but here's my take:
On teams that I work with, we never push to the dev branch. Instead we create
a Pull Request from the feature branch to the dev branch. We also have a requirement that the feature branch must have the most up-to-date version of the dev branch merged into it before a Pull Request is accepted.
In some projects, we take this a step further and eliminate the dev branch. We just have main
. Feature branches are created from main
and Pull Requests are made back to main
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论