使用 GIT,如何有选择地将一个提交的更改合并到另一个“分叉”上?

发布于 2024-08-04 04:47:22 字数 235 浏览 4 评论 0原文

采取这样的场景:

  1. 我决定在 github.com 上“分叉”一个代码库,并开始执行我的例程:编辑 - 提交 - 推送;又名黑客黑客黑客。
  2. 在我做了一些更改之后,我看到其他人对同一个项目做了一些更改,我喜欢它们!
  3. 我决定将它们合并到我的中。问题是,我只想要他所做的多次提交中的一个特定提交的“一部分”。

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

Take this scenario:

  1. I decide to 'fork' a codebase on github.com, and start doing my routine: Edit - Commit - Push; aka hack hack hack.
  2. After I made some changes, I see some changes another person has made on the same project, and I like them!
  3. I decide I want to merge them into mine. Problem is, I only want a 'portion' of one particular commit, out of several commits he has made.

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

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

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

发布评论

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

评论(4

无畏 2024-08-11 04:47:22

在 IRC 世界中掀起波澜之后,有人给出了一个很好的解决方案:

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

希望这可以帮助其他有同样问题的人!

编辑:好吧,也许事情没那么简单。以下是完整步骤:

  1. 首先,您必须位于存储库中,执行必要的 cd-ing 操作以访问您的工作目录。

  2. 您现在需要添加远程分支,然后获取它。通过以下方式执行此操作:

    git 远程添加 someUser git://github.com/someUser/someRepo.git

    git fetch someUser

  3. 您现在可以运行诸如 git log someUser/master 以查找要“部分”合并的提交 SHA1。

  4. 获得 SHA 后,您现在可以运行:

    gitcherry-pick-n SHA1

    其中 SHA1 是提交 SHA,呃!

  5. 很可能会出现冲突,具体取决于提交的时间以及项目特定区域更改的频率。抱歉,您必须通过值得信赖的编辑器手动解决这些问题。因此,拿出 VIM 或您使用的其他任何东西,并解决冲突,直到您到达您喜欢更改的阶段。

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

    git 重置 HEAD

    git add --patchgit add -p

  7. 耶!提交时间:

    git commit -m“我合并了选定数量的更改”

  8. 清理混乱(你在git add --patch) 并且仅将选定的更改保留在工作存储库中,运行:

    git reset --hard HEAD

    显然 git checkout -f 也是另一个选择。

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!

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

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

  2. 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

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

  4. Once you have the SHA, you can now run:

    git cherry-pick -n SHA1

    where SHA1 is the commit SHA, duh!

  5. 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.

  6. 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 --patch or git add -p

  7. Yay! Commit time:

    git commit -m "I merged a select amount of changes"

  8. 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

    Apparently git checkout -f is another option as well.

把回忆走一遍 2024-08-11 04:47:22

我真的很喜欢 Tim 的解决方案,但有时我喜欢在 vimdiff 中进行修改。我对这个问题的解决方案很粗糙,但它对我有用,因为我喜欢 vim。

我将 vimdiff 设置为我的 diff 工具,然后有选择地合并我比较分支:

git difftool <branch> <file>

然后我转到包含当前分支版本的窗格并在 vim 中编辑原始版本(有时这不是必需的,但有时 vimdiff 会在/tmp) 并禁用只读模式:

:e <file>
:set readonly!

现在我可以使用 vim 的修补工具(例如 dodp)来应用我想要的内容,并随时进行其他小编辑。完成后,我保存文件,退出 vim,然后像常规编辑一样在 git 中暂存并提交文件。

正如我所说,这并不是特别复杂,但它非常强大,而且仍然纯粹在命令行中。请务必添加明确的提交消息,因为 git 不会自动为您包含合并消息。

vimdiff 示例

I really like Tim's solution, however sometimes I like tinkering in vimdiff. My solution to this problem is crude, but it works for me because I like vim.

I have vimdiff set as my difftool, and then to selectively merge I diff the branch:

git difftool <branch> <file>

Then I go to the pane with the current branch's version and edit the original in vim (sometimes this isn't necessary, but sometimes vimdiff opens a version in /tmp) and disable read-only mode:

:e <file>
:set readonly!

Now I can use vim's patch tools such as do and dp to apply what I want, and make other little edits as I go. When I am done, I save the file, exit out of vim, and then stage and commit the file in git like a regular edit.

As I said, this isn't particularly sophisticated, but it is very powerful, and still purely in the command line. Just be sure to add a clear commit message, as git won't automatically include a merge message for you.

vimdiff example

凉月流沐 2024-08-11 04:47:22

我不清楚你想要什么。如果您只想引入其他分支的某些提交,可以使用 gitcherry-pick SHA。关于 gitready 的完整说明。

I'm not clear on what you want. If you want to bring in only certain commits from the other forks, you can use git cherry-pick SHA. Full explanation on gitready.

装纯掩盖桑 2024-08-11 04:47:22

如果您只想要一个提交的端口,那么您最好选择您想要的提交并重置您不想触及的文件。

git cherry-pick SHA1
git checkout HEAD file1 file2 ... fileN

当然,如果您在一个文件中有多个修改的部分,并且只想保留其中的一些部分,那么您别无选择,只能手动编辑文件,删除它们的更改。

If you only want a port of a commit, you’re probably best off by cherry-picking the commit you want and resetting files you don’t want touched.

git cherry-pick SHA1
git checkout HEAD file1 file2 ... fileN

Of course, if you have several modified parts in a file and only want to keep some of them you have no choice but to edit the file manually, cutting out their changes.

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