简体   繁体   English

如何在git中将文件历史记录从一个分支合并到另一个分支?

[英]How can I merge a file history from a branch to another in git?

So first check the following image, I tried to represent what I need 因此,首先检查下图,我试图代表我的需求 git情况

The green squares are the commits, and the yellow, brown and red are parts of those commits (in my case commits to specifically 3 files spread through the commits). 绿色方块是提交,黄色,棕色和红色是这些提交的一部分(在我的情况下,具体来说是提交中分布的3个文件)。

There are no further changes made on dev 对开发人员没有进一步的更改

What actually happened: I was working on a branch and found out that some changes were actually useful on the dev branch and were not actually related to the dev2 branch. 实际发生的情况:我在一个分支上工作,发现一些更改实际上对dev分支有用,而实际上与dev2分支dev2 So I wanted that those changes on the dev branch. 所以我希望在dev分支上进行这些更改。

git checkout
git merge

I actually want the modifications to get out of the dev2 branch and go into the dev branch, so if I do the checkout>commit the modifications will be on both branches, that's why I wouldn't like to do it. 我实际上希望修改脱离dev2分支并进入dev分支,所以如果我执行checkout> commit,则修改将在两个分支上进行,这就是为什么我不愿意这样做。

Is this even possible? 这有可能吗? How can I do it? 我该怎么做?

You can just find the sha of the last commit that should be in dev (the red square) by using git log , and then go to your master branch checkout master and just say git merge SHA where SHA of the red square. 您可以使用git log来找到应该在dev(红色方块)中的最后一次提交的sha ,然后转到master分支checkout master然后说git merge SHA ,其中红色方块的SHA。 This is quite a common thing in git. 这在git中很常见。 It happens a lot with forked repos, to merge in the updates of the original. 分叉的回购经常发生,合并到原始版本的更新中。

Remember though that this gets the whole commits, i am not sure if that is what you want. 请记住,尽管这可以获取全部提交,但是我不确定这是否是您想要的。 If you want just some files from those commits, it will be a lot harder. 如果您只想从这些提交中获取一些文件,则要困难得多。 Then you should have a look at this question . 那你应该看看这个问题

Yes, this is possible. 是的,这是可能的。 You need to do three things: 您需要做三件事:

  • Checkout the original branch: git checkout dev 签出原始分支: git checkout dev
  • Apply the commits that you want: git cherry-pick yellow , git cherry-pick brown , git cherry-pick red 应用所需的提交: git cherry-pick yellowgit cherry-pick browngit cherry-pick red
  • Rebase dev2 onto dev : git checkout dev2 , git rebase --onto dev dev2 devgit checkout dev2git rebase --onto dev

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将一个分支中的特定文件合并到Git中的另一个分支中 - How can I merge a specific file from one branch into another branch in Git 我如何从另一个git分支合并,除了一些提交,并且有理智/易于查询的历史记录? - How do I merge from another git branch, excepting some commits, and have sane/easily queried history? 如何在不添加分支的完整提交历史记录的情况下合并另一个仓库中的分支? - How can I merge a branch from another repo without adding the branch's full commit history? 如何在git中将文件从一个分支合并到另一个分支? - How can I merge files from one branch to another branch in git? 如果合并为空,是否可以删除Git历史记录中的悬挂分支? - Can I delete a dangling branch in my Git history if the merge is empty? 如何将1个提交从分支合并到另一个分支 - how can I merge 1 commit from a branch to another branch 如何使用git将另一个分支中的文件签出到当前分支的新文件中? - How can I check out a file from another branch into a new file in the current branch with git? 我如何有选择地合并或从 Git 中的另一个分支中挑选更改? - How can I selectively merge or pick changes from another branch in Git? 如何在分支中重命名文件,合并并在Git中具有历史记录的两个文件? - How to rename file in branch, merge and have two files with history in Git? 当我在另一个本地分支上重新设置本地分支以从其历史记录中排除提交时,为什么会给 Git 带来合并冲突? - Why gives Git a merge conflict, when I rebase a local branch on another local branch to exclude a commit from its history?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM