如何在同一分支工作时自动执行Git拉取

huangapple go评论65阅读模式
英文:

how to git pull automatically when working on same branch

问题

在git flow中,有masterdevfeature/*分支。

开发者在开始工作时应该从dev切换到feature/*分支。

但我发现,如果有人先推送到dev分支,另一个人在正确推送之前应该先拉取dev

所以,步骤如下:

  1. 开发功能并提交。
  2. 切换到dev分支并合并feature/*
  3. 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.

  1. work on feature and commit.
  2. checkout to dev branch and merge feature/*.
  3. 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.

huangapple
  • 本文由 发表于 2023年3月7日 09:21:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/75657252.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定