"拉"来自其他GIT分支

时间:2017-04-25 11:32:38

标签: git version-control

问题来自开发多个非常相似的产品的过程,核心变化必须推到其所有版本。

简短的问题:

什么可以用于从其他GIT分支获取文件?
(例如,由于git pull可用于获取提交,因此需要替代自定义文件

长问题:

GIT层次结构如下:

master  
|── product-A  
└── product-B

product-A 分支上进行了一些更改(提交),在 product-B 上,我们希望更改主要基于 product-A的更改
在不拉动提交的情况下将在一个分支上写入的代码转移到另一个分支的选项有哪些选项,因为在 product-B中进行 product-A 特定提交可能没有意义

理想情况:

  • 我希望有能力解决问题(与git mergetool一样解决代码冲突的行为)
  • 如果文件位于非常相似的产品的另一个存储库中,那么该解决方案也可以工作(有没有比复制和粘贴文件更好的东西?)

2 个答案:

答案 0 :(得分:0)

使用:

# get your files
git cherry-pick -n <commit>

# unstage changes
git reset

# select the changes you need
git add -p

# commit
git commit  

或在Evusas sugested

时将您的更改提交到父分支

答案 1 :(得分:0)

将产品A分支的变更转移到产品B分支,您可以使用:

git checkout product-B
git cherry-pick <commit from product-A> -X theirs|ours

<commit from product-A>product-A分支上的提交,其中包含您要传输的更改。

-X选项,可以自动解决冲突文件。 -X theirs会将冲突文件版本保留为您转移的product-A上的提交。 -X ours会将冲突文件版本保留为product-B分支上的当前版本。

将更改从一个仓库转移到另一个仓库:

# in the current git repo
git remote add another <URL for another repo> -f
gitk --all # find the commit on another/product-A branch which you want to transfer changes
git checkout product-B
git cherry-pick <the commit you found>