Reputation: 5022
I have two repositories, one of which is a clone of the other.
Essentially what happened was there was a first repo created (A) which then has had work and a bunch of history generated, and then a clone was taken by way of file copy but without creating a branch. The copied repo (B) has had changes and commits made and the first repo (A) has had changes and commits made.
I now need to get both repos merged back into one, such that I have all changes made from both merged with conflict resolution if necessary.
How can I go about this?
Upvotes: 2
Views: 138
Reputation: 10053
In case of merging of two repos, which is like one is a modified copy of other.
You can use tools like meld
which will make things very simple. Just add both the folders inside the tool and then keep one as standard. It has very simple options to include changes and push things on one end using clicks. This will make sure none of your changes are missed out and it gives a very nice comparison.
When two branches needs to be merged after a long time, use rebase instead of merge.
Upvotes: 0
Reputation: 13610
Not having created a branch isn't a big problem. If you have common commits then it should be quite easy.
First find the common commit
Create a new branch from A, it should point to the HEAD of your repository A. When merging it it will automatically find where it actually branched. We're saving the HEAD in a different branch to merge in an isolate branch. If you mess up, the original branch with it's commit will still be available and you can simply delete the branch with the messed up merge.
Before doing any merge, I usually save a clean branch of my original commit. I work on an alternative branch that I can move (reset) to any place and then when the work is done. I'd replace the original branch with the merged one. You can see it as a checkpoint. If you mess something. Go back to the checkpoint and start again. If you have really complicated merge, you can save as many checkpoints as you want. You'll simply have to clean up later.
git checkout -b "MergeAB"
Second you'll have to get the code from the repository B into A
Usually it should be possible to just fetch from B where "../B" is the path to your B repository. It will fetch in a branch named "FETCH_HEAD".
git fetch ../B
From ../B
* branch HEAD -> FETCH_HEAD
Third merge
Simply merge the FETCH_HEAD branch into your working branch and then if the merge succeed. You could reset the original branch to the MergeAB
branch and delete MergeAB
.
git merge FETCH_HEAD
Finally
Resolve conflicts
Note:
I'm not so sure but it is possible that merging B into A might give different result than merging A into B. I know that merge can be difficult when some changes are appearing in different orders. If a person copied a old file into his repository after a change was made. The old file commited after new changes could end up looking like changes that were reverted to the old version.
I first wrote to use reset
but it isn't necessary. If you lost your last commit for whatever reason. This might help you find it back.
git fsck --lost-found | grep commit | cut -f3 -d" " | xargs -n1 git log -n1
It's going to display log for all "dangling" commit (without a branch). Even if the commit might look deleted. It should be possible to see them in any good viewer with their "hash".
Upvotes: 1