简体   繁体   English

分支的git merge子文件夹

[英]git merge subfolder of branch

I'm pretty sure this situation is quite common, but I haven't found any information on how to deal with it in git. 我敢肯定这种情况很普遍,但是我还没有在git中找到有关如何处理它的任何信息。


Here's what's happened so far: 到目前为止,这是发生了什么:

Imagine the following folder structure in a git repo: 想象一下git repo中的以下文件夹结构:

/server
/client
.gitignore

The repo contains both the server and client code of a Client-Server application. 该回购包含客户端服务器应用程序的服务器代码和客户端代码。

This is the revision history of the master branch: 这是master分支的修订历史记录:

a01... (changes /server content)
b02... (changes /client content)
c03... (changes /server content)
d04... (changes /client content)
c05... (changes /server content)
d06... (changes /client content)

At this point, we pick the commit b02 and create a new branch server_v2 out of it. 此时,我们选择提交b02并在其中创建一个新的分支server_v2 On that branch, we make two commits e07.. and e08.. . 在该分支上,我们进行两次提交e07..e08.. Here's the revision history of server_v2 : 这是server_v2的修订历史:

a01... (changes /server content)
b02... (changes /client content)  <---- here, master and server_v2 are forked
e07... (changes /server content)
e08... (changes /server content)

And here's the revision graph: 这是修订图:

a01
b02
 * *
 *    *
 *       *
 *          *
c03            *
d04               *
c05                  *
d06 <-- master          *
                       e07
                       e08 <-- server_v2

Note that all commits affect only one of the subfolders, and the first character of the commit hash indicates which one: 请注意,所有提交仅影响一个子文件夹,并且提交哈希的第一个字符指示哪个:

a: commit on /server, before the fork
b: commit on /client, before the fork
c: commit on /server, only in master
d: commit on /client, only in master
e: commit on /server, only in server_v2

Here's what we're trying to do: 这是我们正在尝试做的事情:

Now we want to put version 2.0 of the server online. 现在,我们要使服务器的2.0版联机。 So we want master to contain the e07 and e08 commits instead of the c03 and c05 commits. 因此,我们希望master包含e07e08提交,而不是c03c05提交。 This is what we want the branches to look like: 这就是我们希望分支看起来像的样子:

master:     a01, b02, d04, d06, e07, e08
server_v1:  a01, b02, c03, c05

In other words, master should contain the new server ( e07 and e08 ) as well as the changes on the client ( d04 and d06 ). 换句话说, master应该包含新服务器( e07e08 )以及客户端上的更改( d04d06 )。 server_v1 should contain the old server ( c03 and c05 ). server_v1应该包含旧服务器( c03c05 )。

Option 1: 选项1:

We could rename the branches: 我们可以重命名分支:

master    --> server_v1
server_v2 --> master

Now master indeed contains the new server version. 现在master确实包含新的服务器版本。

Problem: We lose the d04 and d06 commits on /client . 问题:我们丢失了/client上的d04d06提交。

Option 2: 选项2:

Merge server_v2 into master . server_v2合并到master Now the revision graph looks like this: 现在,修订图如下所示:

a01
b02
 * *
 *    *
 *       *
 *          *
c03            *
d04               *
c05                  *
d06                     *
 *                     e07
 *                     e08 <-- server_v2
 *                   *
 *               *
 *           *
 *       *
 *   *
[merge] <-- master

Problem: This way, the c03 and c05 commits are part of master . 问题:这样, c03c05提交是master一部分。 We don't want the changes on the old server to be part of the new master branch. 我们不希望旧服务器上的更改成为新的master分支的一部分。

Is there a way to achieve this? 有没有办法做到这一点?

git checkout master

git checkout -b server_v1 ## create server_v1 branch as off the current master

git checkout master
git revert c03  ##revert commits you don't need
git revert c05 

git merge server_v2 ##merge your server_v2 changes into master (you can also rebase server_v2 onto master and fast-forward master to server_v2 if you want linear history)

That's it. 而已。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM