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.
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.
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.
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.
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>/
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With