使用 GIT,如何有选择地合并来自另一个“分叉"上的一个提交的更改? [英] Using GIT, how can I selectively merge changes from one commit on another 'fork'?

查看:20
本文介绍了使用 GIT,如何有选择地合并来自另一个“分叉"上的一个提交的更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以这个场景为例:

  1. 我决定在 github.com 上分叉"一个代码库,然后开始做我的日常工作:编辑 - 提交 - 推送;又名 hack hack hack.
  2. 在我做了一些改变之后,我看到另一个人在同一个项目上做了一些改变,我喜欢他们!
  3. 我决定将它们合并到我的中.问题是,我只想要他所做的几次提交中的一个特定提交的部分".

将这些选定数量的更改合并到我的分叉"中的最有效方法是什么?

What would be the most efficient method of getting these select amount of changes, merged into my 'fork'?

推荐答案

在 IRC 世界的波涛汹涌之后,有人给出了一个很好的解决方案:

After trolling the waves of the IRC world, someone gave a great solution:

git cherry-pick SHA1 --no-commit
git add --patch

希望对有同样问题的其他人有所帮助!

Hopefully that helps anyone else with the same question!

好的,也许没那么简单.以下是完整步骤:

OK, maybe it isn't quite that simple. Here are the full steps:

  1. 首先,您必须在您的存储库中,执行必要的 cd-ing 以进入您的工作目录.

  1. First you have to be in your repository, do the necessary cd-ing to get to your working directory.

您现在需要添加远程分支,然后获取它.这样做:

You now need to add the remote branch, and then fetch it. Do this by:

git remote add someUser git://github.com/someUser/someRepo.git

git fetch someUser

您现在可以运行诸如 git log someUser/master 之类的命令来查找要部分"合并的提交 SHA1.

You can now run commands such as git log someUser/master to find the commit SHA1 you want to merge in 'partially'.

拥有 SHA 后,您现在可以运行:

Once you have the SHA, you can now run:

git cherry-pick -n SHA1

其中 SHA1 是提交 SHA,呵呵!

where SHA1 is the commit SHA, duh!

很可能会发生冲突,这取决于提交的时间以及项目特定区域的更改频率.抱歉,您必须使用可信赖的编辑器手动解决这些问题.因此,请退出 VIM 或其他任何您使用的工具,并消除冲突,直到达到您喜欢更改的阶段.

There quite possibly will be conflicts, depending how old the commit is, and how often that particular area of the project changes. Sorry, but you have to manually resolve these with your trusty editor. So pull out VIM, or whatever else you use, and hack away at the conflicts until you get to the stage where you like the changes.

您现在必须将索引重置为 HEAD 修订版,然后您可以使用可信赖的 GIT add --patch 命令来选择您想要的更改:

You now have to reset the index to the HEAD revision, then you can then use the trusty GIT add --patch command to pick and choose what changes you want:

git reset HEAD

git add --patchgit add -p

耶!提交时间:

git commit -m "我合并了一定数量的更改"

要清理混乱(您在 git add --patch 中拒绝的内容)并仅将选定的更改保留在您的工作存储库中,请运行:

To clean up the mess (The stuff you said no to in git add --patch) and only keep the selected changes in your working repository, run:

git reset --hard HEAD

显然 git checkout -f 也是另一种选择.

Apparently git checkout -f is another option as well.

这篇关于使用 GIT,如何有选择地合并来自另一个“分叉"上的一个提交的更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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