首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Git中合并子目录?

如何在Git中合并子目录?
EN

Stack Overflow用户
提问于 2009-07-31 21:07:33
回答 6查看 92.5K关注 0票数 104

是否可以将本地Git分支中子目录的更改合并到远程Git分支,或者是“全部或全部不合并”?

例如,我有:

代码语言:javascript
复制
branch-a
 - content-1
 - dir-1
   - content-2

代码语言:javascript
复制
branch-b
 - content-1
 - dir-1
   - `content-2

我只想将branch-a dir-1的内容与branch-b dir-1的内容合并。

EN

回答 6

Stack Overflow用户

发布于 2014-09-09 23:57:47

对于我的例子,假设你有一个分支'source‘和一个分支'destination’,这两个分支都反映了它们自身的上游版本(或者不是,如果只是本地的话),并被拉到最新的代码。假设我想要存储库中名为newFeature的子目录,它只存在于“源”分支中。

代码语言:javascript
复制
git checkout destination
git checkout source newFeature/
git commit -am "Merged the new feature from source to destination branch."
git pull --rebase
git push

它比我见过的任何其他东西都要简单得多,这对我来说是完美的,found here

请注意,这不是“真正的合并”,因此在目标分支中不会有关于newFeature的提交信息,只有对该子目录中文件的修改。但是,由于您可能会在稍后合并整个分支,或者将其丢弃,所以这可能不是问题。

票数 36
EN

Stack Overflow用户

发布于 2020-03-20 20:32:01

假设操作员有两个分支,但是只想将-1\f25-1\f6分支-1\f25 a-1\f6中的-1\f25-1\f6合并到-1\f25-1\f6分支-b-1\f25-1\f6中

代码语言:javascript
复制
# Make sure you are in the branch with the changes you want
git checkout branch-a

# Split the desired folder into its own temporary branch
# This replays all commits, so it could take a while
git subtree split -P dir-1 -b temp-branch

# Enter the branch where you want to merge the desired changes into
git checkout branch-b

# Merge the changes from the temporary branch
git subtree merge -P dir-1 temp-branch

# Handle any conflicts
git mergetool

# Commit
git commit -am "Merged dir-1 changes from branch-a"

# Delete temp-branch
git branch -d temp-branch
票数 15
EN

Stack Overflow用户

发布于 2012-09-18 17:07:46

这是我从Eclipse的一个forum thread那里得到的,它就像一个护身符:

代码语言:javascript
复制
git checkout source-branch
git checkout target-branch <directories-or-files-you-do-**NOT**-want> 
git commit
git checkout target-branch
git merge source-branch
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1214906

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档