4

I want to merge and decouple from a remote repository as subdirectory with full history. There are several ways and questions how to do that.
My first try was to use subtree but it seems not to rewrite the history of the files, so I can't look into the history of the merged repository.

Next try was to manual merge it like Seth Robertson shown up in his answer:

And the trick to making this work: force Git to recognize the rename by creating a subdirectory and moving the contents into it.

mkdir bdir
git mv B bdir
git commit -a -m bdir-rename

Return to repository "A" and fetch and merge the contents of "B":

cd ../a
git remote add -f B ../b
git merge -s ours --no-commit B/master
git read-tree --prefix= -u B/master
git commit -m "subtree merge B into bdir"

To show that they're now merged:

cd bdir
echo BBB>>B
git commit -a -m BBB

To prove the full history is preserved in a connected chain:

git log --follow B

Works fine so far, but it seems that most tools like doesn't use the follow option for git log.

So I need another way to do this. Can anyone tell me a way which works without renaming and keeping the history?

Thanks in advance.

6
  • From the same linked in answer look at the filter-branch options.
    – Andrew C
    Sep 23, 2014 at 14:24
  • @AndrewC Uhm ... The linked answer doesn't contain anything about filter-branch options. I can't follow what you exactly mean.
    – CSchulz
    Sep 23, 2014 at 14:37
  • Did you see git-scm.com/book/en/Git-Tools-Subtree-Merging ? Sep 23, 2014 at 15:23
  • Yes I have seen it, but I don't know how it can help me in my case.
    – CSchulz
    Sep 23, 2014 at 15:40
  • The question itself has the filter branch. If you don't want the files to have 'moved' you need to make them appear as if they were at the new location at every point in history, you use filter-branch for that
    – Andrew C
    Sep 23, 2014 at 15:43

3 Answers 3

4

Finally the solution is "simple" to import repo B as subdirectory of repo A. I have found by accident a small tool, which contains a description how to use git filter-branch.

git filter-branch --index-filter \
  'git ls-files -s | sed "s-\t\"*-&newsubdir/-" | GIT_INDEX_FILE=$GIT_INDEX_FILE.new git update-index --index-info && mv "$GIT_INDEX_FILE.new" "$GIT_INDEX_FILE"' \
  --tag-name-filter cat \
  -- --all

The complete way is to use the above posted git filter-branch on repo B and after that run the following commands on repo A to import the stuff:

git remote add -f B ../b
git merge -s ours --no-commit B/master
git read-tree --prefix= -u B/master
git commit -m "subtree merge B into bdir"
4

Both solutions in the question are subtree merges. And indeed to simplify inspecting history in the target repository, you need to rewrite history in the source one, and do the merge. But filter-branch is discouraged.

Say you want to merge repository a into b (I'm assuming they're located alongside one another):

cd a
git filter-repo --to-subdirectory-filter a
cd ..
cd b
git remote add a ../a
git fetch a
git merge --allow-unrelated-histories a/master
git remote remove a

For this you need git-filter-repo installed.

An example of merging 2 big repositories, putting one of them into a subdirectory: https://gist.github.com/x-yuri/9890ab1079cf4357d6f269d073fd9731

More on it here.

0

As discussed later in the comments in stackoverflow.com#6442034, the solution in @CSchulz' stackoverflow.com#26001954/577001 (thanks!) may fail on newer platforms. Also some symbols could be explained better (e.g. "B", "b", "master"). So I'm attempting to make those self-explanatory.

export SUBDIR_TOBE_IMPORTED=subdir_tobe_imported
export BRANCH_ON_GITREPO_TOBE_IMPORTED=branch_on_gitrepo_tobe_imported
export GITREPO_DEST=repo_dest
export GITREPO_DEST_PATH=<% Absolute path to $GITREPO_SOURCE %>
export GITREPO_SOURCE=name_gitrepo_tobe_imported
export GITREPO_DEST_PATH=<% Absolute path to $GITREPO_SOURCE %>

cd $GITREPO_DEST_PATH
git filter-branch --index-filter \
  'git ls-files -s | sed "s-\t\"*-&$SUBDIR_TOBE_IMPORTED/-" | GIT_INDEX_FILE=$GIT_INDEX_FILE.new git update-index --index-info && mv "$GIT_INDEX_FILE.new" "$GIT_INDEX_FILE"' \
  --tag-name-filter cat \
  -- --all

cd $GITREPO_DEST_PATH
mkdir $SUBDIR_TOBE_IMPORTED && git add $SUBDIR_TOBE_IMPORTED && git commit -m "Commiting a subdir to be imported. Without this, 'git merge' later may fail"
git remote add -f $GITREPO_SOURCE ../$GITREPO_SOURCE
git merge -s ours --allow-unrelated-histories --no-commit $GITREPO_SOURCE/$BRANCH_ON_GITREPO_TOBE_IMPORTED
git read-tree --prefix=$SUBDIR_TOBE_IMPORTED -u $GITREPO_SOURCE/$BRANCH_ON_GITREPO_TOBE_IMPORTED
git commit -m "Merge subdir from source repo into dest repo."

echo "Optionally I thought of squashing the 2 commits, as 1st commit made above is absolutely dummy, but I found the 2nd commit includes the entire imported history. Since squashing them would almost invalidate all the effort up until this point, you don't want to squash."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.