9

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.

Aaghaaz
  • 93
  • 1
  • 4

2 Answers2

4

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>/

Saurabh P Bhandari
  • 4,338
  • 1
  • 7
  • 34
1

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.

VonC
  • 1,042,979
  • 435
  • 3,649
  • 4,283