从一个分支中提取所有提交,将指定的提交推送到另一个分支

发布于 2024-07-22 07:19:14 字数 623 浏览 5 评论 0原文

我有以下分支:

  • master
  • product

和以下远程分支:

  • origin/master
  • origin/product

我有一个脚本,用于获取 origin/master 分支并获取与上次获取相比发生的更改的差异 (log -p master..origin/master)。 然后我合并origin/master

找到的提交被推送到代码审查工具。

我想将成功的提交(并且仅将它们)推送到生产分支,当然还有 origin/product

我怎样才能这样做呢?

另外,我正在运行 2 个脚本:一个从 origin/master 获取,将详细信息推送到数据库并合并,另一个是我当前正在编写的,必须推送成功承诺。

我想让这两个脚本运行,同时避免竞争条件/合并冲突。 由于我只想使用指定的提交,也许有一种方法可以删除我不想要的提交?

I have the following branches:

  • master
  • production

and the following remote branches:

  • origin/master
  • origin/production

I have a script that fetches the origin/master branch and gets the diff of what changed from my last fetch (log -p master..origin/master). Then I merge origin/master.

The commits found are pushed to a code review tool.

I want to push the successful commits – and only them – to the production branch, and then of course to origin/production.

How can I do so?

Also, I have 2 scripts running: the one that fetch from origin/master, push commits details to a database, and merge, and the other that I'm currently writing that will have to push the successful commits.

I'd like to have those 2 scripts running while avoiding race conditions/merge conflict. Since I only want to work with specified commits, maybe there's a way to get rid of the commits that I don't want?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

动听の歌 2024-07-29 07:19:14

我认为您正在寻找的术语是“樱桃选择”。 也就是说,从一个分支的中间获取单个提交并将其添加到另一个分支:

A-----B------C
 \
  \
   D

成为

A-----B------C
 \
  \
   D-----C'

这当然可以使用 gitcherry-pick 命令来完成。

此提交的问题是 git 认为提交包含之前的所有历史记录 - 因此,如果您有三个提交,如下所示:

A-----B-----C

并尝试摆脱 B,您必须创建一个全新的提交,如下所示:

A-----------C'

其中 C' 有不同的 SHA-1 ID。 同样,从一个分支到另一个分支的挑选提交基本上涉及生成补丁,然后应用它,从而也会以这种方式丢失历史记录。

提交 ID 的这种更改破坏了 git 的合并功能(尽管如果谨慎使用,会有一些启发式方法来掩盖这一点)。 但更重要的是,它忽略了函数依赖性 - 如果 C 实际上使用了 B 中定义的函数,你永远不会知道。

也许处理这个问题的更好方法是拥有更细粒度的分支。 也就是说,不仅仅是有一个“master”,还有“featureA”、“bugfixB”等。一次对整个分支执行代码审查 - 每个分支都非常专注于只做一件事 - 然后合并它当你完成后,一个分支。 这就是 git 设计的工作流程,也是它擅长的地方:)

如果您坚持在补丁级别处理事情,您可能需要看看 darcs - 它认为存储库是一组补丁,并且因此,采摘樱桃成为基本操作。 然而,这有它自己的一系列问题,例如非常慢:)

编辑:另外,我不确定我是否理解你的第二个问题,关于这两个脚本。 也许您可以更详细地描述它,可能作为一个单独的问题来防止事情变得混乱?

The term I think you're looking for is a 'cherry pick'. That is, take a single commit from the middle of one branch and add it to another:

A-----B------C
 \
  \
   D

becomes

A-----B------C
 \
  \
   D-----C'

This, of course, can be done with the git cherry-pick command.

The problem with this commit is that git considers commits to include all history before them - thus, if you have three commits like so:

A-----B-----C

And try to get rid of B, you have to create an entirely new commit like so:

A-----------C'

Where C' has a different SHA-1 ID. Likewise, cherry picking a commit from one branch to another basically involves generating a patch, then applying it, thus losing history that way as well.

This changing of commit IDs breaks git's merging functionality among other things (though if used sparingly there are heuristics that will paper over this). More importantly though, it ignores functional dependencies - if C actually used a function defined in B, you'll never know.

Perhaps a better way to handle this would be to have more fine grained branches. That is, instead of just having a 'master', have 'featureA', 'bugfixB', etc. Perform code review on an entire branch at a time - where each branch is very focused on doing only one thing - and then merge that one branch when you're done. This is the workflow that git is designed for, and what it's good at :)

If you insist on dealing with things at the level of patches, you may want to look at darcs - it considers a repository to be a set of patches, and thus cherry picking becomes the fundamental operation. However this has its own set of problems, such as being very slow :)

Edit: Also, I'm not sure I understand your second question, about the two scripts. Maybe you could describe it in more detail, possibly as a separate question to keep things from getting confusing?

谈场末日恋爱 2024-07-29 07:19:14

我意识到这是一个老问题,但在这里引用: 如何在 Git 中合并特定提交

因此,一个更新的答案是:使用功能分支和拉取请求。

这看起来像什么,其中 fA 是功能 A 的提交,fB 是功能 B 的提交:

            fA   fC (bad commit, don't merge)
           /  \ /
master ----A----B----C
                \  /
                 fB

拉取请求与 GitHub 的功能相关联,但我真正的意思是有人有责任将功能分支合并到 master 中。

I realise this is an old question, but is referenced here: How to merge a specific commit in Git

Hence, a newer answer: Use feature branches and pull requests.

What this looks like, where fA is a commit with feature A, and fB is a commit with feature B:

            fA   fC (bad commit, don't merge)
           /  \ /
master ----A----B----C
                \  /
                 fB

Pull requests are associated with GitHub's functionality, but really all I mean is that someone has the responsibility of merging the feature branches into master.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文