AdrianO
AdrianO

Reputation: 175

How to git-merge two diverged branches side-by-side into one branch?

I have two git branches which diverged from a common commit, say branch A and branch B:

      -- a -- a' -- a'' (branch A)
    /
-- o -- b -- b' -- b'' (branch B)

Both commits a'' and b'' contain a lot of similar files which are now used for different applications though. I want to keep both file structures in a'' and b''.

Now, I would like to merge the two by adding the contents of B to A into a subdirectory.

A sample situation would be script.py in commit o which contains print ('original'). In branch A, the file gets changed to print ('some feature A') throughout a series of commits; while in branch B, the file gets changed to print ('another feature B') also throughout some commits.

How would I now end up in a branch where I have the files

script.py   (from A)
B/script.py   (from B)

with print ('some feature A') and print ('another feature B') as contents, respectively?

Note: I want to keep the incremental histories for both the A version of script.py and the B version B/script.py case to be able to git blame either file.

Upvotes: 1

Views: 548

Answers (1)

OmeGak
OmeGak

Reputation: 290

I think what you want is git subtree. In particular, this command will do exactly what you describe.

git subtree --prefix=B B

Assuming you run this command in branch A, it will create a subdirectory B with the whole codebase existing in branch B. History will be preserved and prefixed and the graph will look like this (m for merge commit).

-- o -- a -- a' -- a'' -- m (branch A)
    \                    /
      - b -- b' -- b'' -   (branch B)

Upvotes: 1

Related Questions