1

I've got a git repo, let's say "projectroot". Now I want to merge from an upstream remote. I want to merge this content into a specific directory of my "projectroot". For instance, into "projectroot/html".

How can I do this? If I just do git merge upstream/branchname it will merge it into the "projectroot" directory. But it has to go in "projectroot/html".

It's important to keep the "projectroot" as my git repo.

Kind regards

Tim Reynaert
  • 145
  • 5
  • 19
  • Is the upstream repository the one for you project or another remote repository with unrelated history? – Simon Doppler Apr 02 '19 at 13:11
  • It's another repo than the one on the rootfolder, but the content inside the /html folder comes from that upstream remote, which was moved to the html folder – Tim Reynaert Apr 02 '19 at 13:17
  • 1
    Have you checked out git submodules? https://git-scm.com/book/en/v2/Git-Tools-Submodules – Mike Faber Apr 02 '19 at 13:26
  • As Mike said, you can use git submodules or [git subtrees](https://www.atlassian.com/blog/git/alternatives-to-git-submodule-git-subtree) – Simon Doppler Apr 02 '19 at 13:27
  • Possible duplicate of [How do you merge two Git repositories?](https://stackoverflow.com/questions/1425892/how-do-you-merge-two-git-repositories) – phd Apr 02 '19 at 18:23
  • https://stackoverflow.com/search?q=%5Bgit%5D+merge+into+subdirectory – phd Apr 02 '19 at 18:23

1 Answers1

1

Suppose you have 2 repositories Repo1 and Repo2 and you want to move Repo2 as subdirectory of Repo1.

To achieve that we will move the content of Repo2 into a subfolder under Repo2 and then merge Repo1 and Repo2.

Below are the detailed steps:

  1. Clone both repo on your machine

    $ git clone Repo1

    $ git clone Repo2

  2. Copy the content of Repo2 to a subfolder. Go to the folder Repo2 and apply the following steps: Create a subfolder Repo2

    $ mkdir Repo2

  3. Move everything from the parent Repo2 to the child Repo2 (except the .git folder) and stage the files (the added folder and deleted file(s)) for a later commit

    $ git stage Repo2/

    $ git stage README.md

  4. Commit and push those changes to git

    $ git commit -am '[REPO-2] Move content to a subfolder'

    $ git push origin master

  5. Go to the folder Repo1 and do the following: Add a remote branch with the content of Repo2

    $ git remote add Repo2Temp (path_to_Repo2)

  6. Fetch Repo2Temp, the temp repo we created in the previous step

    $ git fetch Repo2Temp

  7. Merge Repo2Temp with Repo1

    $ git merge Repo2Temp/master

  8. delete the remote Repo2Temp

    $ git remote rm Repo2Temp

  9. Push the changes to the server

    $ git push origin master

Gary Mendonca
  • 1,925
  • 1
  • 13
  • 21