如何在 Git 中合并特定提交 [英] How to merge a specific commit in Git

查看:47
本文介绍了如何在 Git 中合并特定提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从 GitHub 的存储库中创建了一个分支,并提交了一些特定于我的内容.现在我发现原始存储库有一个很好的特性,它位于 HEAD.

我只想在没有先前提交的情况下合并它.我该怎么办?我知道如何合并所有提交:

git branch -b a-good-featuregit pull 仓库主git 结帐大师git合并一个好功能git commit -a推

解决方案

'git cherry-pick' 应该是你的答案.

<块引用>

应用由现有提交引入的更改.

不要忘记阅读 bdonlan 在这篇文章中关于樱桃采摘后果的回答:
"从分支,将指定的提交推送到另一个",其中:

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

变成:

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

<块引用>

这个提交的问题在于 git 认为提交包含了它们之前的所有历史

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

这种提交 ID 的更改破坏了 git 的合并功能以及其他功能(尽管如果谨慎使用,会有一些启发式方法可以掩盖这一点).
更重要的是,它忽略了函数依赖——如果 C 真的使用了 B 中定义的函数,你永远不会知道.

I have forked a branch from a repository in GitHub and committed something specific to me. Now I found the original repository had a good feature which was at HEAD.

I want to merge it only without previous commits. What should I do? I know how to merge all commits:

git branch -b a-good-feature
git pull repository master
git checkout master
git merge a-good-feature
git commit -a
git push

解决方案

'git cherry-pick' should be your answer here.

Apply the change introduced by an existing commit.

Do not forget to read bdonlan's answer about the consequence of cherry-picking in this post:
"Pull all commits from a branch, push specified commits to another", where:

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

becomes:

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

The problem with this commit is that git considers commits to include all history before them

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.

这篇关于如何在 Git 中合并特定提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆