Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use git-filter-repo to merge one repo as subdirectory into another

While trying to use git filter-branch to merge repos using this gist. Adding just the relevant snippet in question:

git filter-branch -f --prune-empty --tree-filter '
        mkdir -p "${REPO_NAME}_tmp"
        git ls-tree --name-only $GIT_COMMIT | xargs -I{} mv {} "${REPO_NAME}_tmp"
        mv "${REPO_NAME}_tmp" "$REPO_NAME"
    '

I got a warning from git stating the following:

WARNING: git-filter-branch has a glut of gotchas generating mangled history
     rewrites.  Hit Ctrl-C before proceeding to abort, then use an
     alternative filtering tool such as 'git filter-repo'
     (https://github.com/newren/git-filter-repo/) instead.  See the
     filter-branch manual page for more details; to squelch this warning,
     set FILTER_BRANCH_SQUELCH_WARNING=1.

So, I took a look at git filter-repo, the description states the following:

restructuring the file layout (such as moving all files into a subdirectory in preparation for merging with another repo, ...

This showed that this issue can be resolved with git filter-repo, but even after checking the documentation and given examples I could not find a way to achieve this. Can someone please help me with the same.

like image 983
Aaghaaz Avatar asked Apr 25 '20 03:04

Aaghaaz


People also ask

Can I merge 2 repos?

You can merge repository A into a subdirectory of a project B using the subtree merge strategy. This is described in Subtree Merging and You by Markus Prinz. (Option --allow-unrelated-histories is needed for Git >= 2.9.

What is git filter repo?

There is a tool called git-filter-repo that you can use to rewrite your Git history and remove a file from every commit that it was involved with. You end up with a repository without certain files or folders, but everyone in the team needs to throw away their current repository and clone it again.

How do I merge two GitHub repository branches?

Merging Branches to Remote Repository Before you can push the branch code in the remote repository, you set the remote repository as the upstream branch using the git push command. This command simultaneously sets the upstream branch and pushes the branch contents to the remote repository.


2 Answers

Replace the filter-branch command in the script

git filter-branch -f --prune-empty --tree-filter '
        mkdir -p "${REPO_NAME}_tmp"
        git ls-tree --name-only $GIT_COMMIT | xargs -I{} mv {} "${REPO_NAME}_tmp"
        mv "${REPO_NAME}_tmp" "$REPO_NAME"
    '

with this

git filter-repo --to-subdirectory-filter "$REPO_NAME"

See Path shortcuts section here

--to-subdirectory-filter <directory>

Treat the project root as instead being under <directory>

Equivalent to using --path-rename :<directory>/

like image 199
Saurabh P Bhandari Avatar answered Sep 22 '22 23:09

Saurabh P Bhandari


This looks like a path-rename, as part of the path-based filters:

cd  $REPO_DIR_TMP
git filter-repo  --path-rename /:${REPO_NAME}_tmp/

That would rewrite the history of the second repo in a subfolder (within that second repo)

Then you can add it as a remote of the first repo, fetch and merge, as in your gitst.

like image 32
VonC Avatar answered Sep 20 '22 23:09

VonC