简体   繁体   中英

How to merge two parallel branches in a Git repository?

I have a project for which there was no Git repository, so I created one for it in Gitlab . At the time of repo creation, I had two folders, one containting the production code and one containing the development code.

I created a local git repo in the folder containing the production code. I pushed the code to the remote master of the repository which I initially created.

I then created a local git repo in the folder containing the development code. I created a new local branch (let's say xyz) from local master. I pushed this new local branch xyz to the same remote repository.

The remote repository now has master branch and the xyz branch. Now, I want to merge the xyz branch to the master as I am finished with the development.

But as I was trying to be oversmart, I have two parallel branches with unrelated histories in the same repository. One solution could be to delete the repository and start fresh with the new code which I just finished with but is there any way to preserve the old code and also merge these branches?

If I understand correctly, you have a single revision in master (or it could be many revisions, doesn't matter) with last production environment.... then you have a branch that is parallel to master where from the very first revision is a continuation of master. is that right? So if we were able to put the first (oldest) revision of xyz (the content of that revision) on top of master and then replay all the other revisions following the first revision of xyz, you would be happy right?

Ok... if that's the case:

git checkout first-revision-xyz # check out the first revision of xyz
git reset --soft master # set the branch pointer on top of the current position of master (master won't be touched, don't worry)
git commit -m "taking back content to how it is on the first revision of xyz" # create a new revision, set appropriate comment
git cherry-pick first-revision-of-xyz..xyz # replay all the other revisions up to the last revision of xyz
# if you like the result
git checkout -b development

There you created branch development, it follows master, you could then start playing with it as usual.

As a different strategy, you might want to merge both branches but keeping the tree (files and content) as it is on xyz? That can be done without going through a real merge process.

git checkout -b test $( git commit-tree -p master -p xyz -m "Merge of prod an development" ${xyz^tree} )

Created a test branch that has the merge of both branches keeping the tree as it is on xyz.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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