How to combine a git nested repository into a parent repo while keeping a story?

Now I know that most git experts will immediately think about git rebase , but I use the word "rebase" in a more general sense: I have the following structure for my project:

 . .. .git tools lib src .git build 

Both directories . and ./src , apparently, are git repositories and have literally a long history and a large number of commits. Repository c . ignores the src directory (which is its own repo).

I just realized that instead I just want to have only one repo in . tracking everything, including source files, because, frankly, the build system is developing along with the source code and has become quite extensive.

My problem is that I do not know how to save this repository in the history, which is now part of the repository in src . Is it possible? This is what I meant by β€œrebasing” - if changes in ./src/main.c tracked by ./src/.git through some N commits, then I want to save these changes and include them in a new repository ./.git . Same story, reinstalled file path.

UPDATE

Merging subtrogs is something other than what I want from what I put together on SO. In short, he does much more than I need. I just need the contents of the old repo, as well as all development branches , and all commits, tags, etc. They look as if they were always part of the parental repo. In essence, the only change is the paths to the files themselves - where ./main.c tracked to the children's repo, the new repo will now track ./src/main.c , and since, as I heard, git tracking content, not files, and then changes the file paths as described above, and the links to these paths should be pretty trivial, correct?

+5
source share
5 answers

Quick and easy way:

Rename all files to src so that they start with src/ . Add src repo as remote, select and merge. Drop the old src repo, now everything is in ./ .

This will leave you with this action recorded in the story.

Rewrite History:

To make this merge invisible, you need to use git filter-branch --tree-filter to add the src/ prefix to the src/ repository. Then add this as a remote to the ./ repository and extract it (until there is a merge). To combine the story well, you need to reorder the commits. Use git log --date-order master src/master to retrieve these commits in the correct order and select them:

 git checkout -b new-master your-first-commit git log --format='%H' --date-order --reverse master src/master | xargs git cherry-pick 

It basically combines sorting by your commits and builds them up to a linear history.

This will not save your merges, so do not do this unless you have a flat story. In this case, use only the filter branch, and then do a normal merge.

+1
source

Cannot be used if it works, but worth a try. Assuming src is on the remote server and the parent is on the remote server. You can try the following.

  • git Clone the remote server url to clone repo using tools, lib and build
  • git add remote src url for remote src repo
  • create an src remote repo tracking branch and then check it on the parent repo
  • Merge the remote tracking branch into your main branch.
  • remove the second remote.
0
source

Use a subtree merge.

I would name the steps, but they answer this question: How do you combine the two git repositories?

0
source

I needed to do the same and it worked for me:

From the parent repo,

 git remote add -f subrepo git@github.com :sub/repo.git git merge -s ours --no-commit subrepo/master git read-tree --prefix=subrepo/ -u subrepo/master git commit -m "Subtree merged" 

This is a GitHub post that also contains more details: https://help.github.com/articles/about-git-subtree-merges/

0
source

Follow the instructions for embedding one repo into another repo, having one Git story, combining both git stories.

  1. Clone both repositories that you want to merge.

git clone git@github.com : user / parent-repo.git

git clone git@github.com : user / child-repo.git

  1. Go to child repo

cd child-repo /

  1. run the command below, replace the path my/new/subdir (3 entries) with the directory structure where you want to have a child repo.

git filter-branch - -p rune -e mpty --tree-filter 'if [! -e my / new / subdir]; then mkdir -p my / new / subdir git ls-tree --name-only $ GIT_COMMIT | xargs -I files mv files my / new / subdir fi '

  1. Go to parent repo

cd ../ parent-repo /

  1. Adding a remote parent repo indicating the path to the child repo

git remote add child-remote ../ child-repo /

  1. Get affiliate repo

git fetch child-remote

  1. Combine stories

git merge --allow-unrelated-history child-remote / master

If you now check the git log in the parent repo, it will have to merge the child repo. You can also see the tag pointing from the commit source.

The article below helped me integrate one repo into another repo, having one single Git story, combining both git stories.

http://ericlathrop.com/2014/01/combining-git-repositories/

Hope this helps. Happy coding!

0
source

Source: https://habr.com/ru/post/1201023/


All Articles