Skip to content

Instantly share code, notes, and snippets.

@korya
Last active August 29, 2015 14:15
Show Gist options
  • Select an option

  • Save korya/a5fcb8a22cec23ffb59d to your computer and use it in GitHub Desktop.

Select an option

Save korya/a5fcb8a22cec23ffb59d to your computer and use it in GitHub Desktop.
Merge a git repo as a subdir

In one of my previous gists, there are instructions for converting a repo subdir to a separate repo. In this git we'll go in opposite direction. Suppose we have 2 git repositories A and B.

Problem

Merge B repo into A as dir b/.

Solution

# Clone A
git clone https://github.com/korya/A
cd A

# Add B as remote
git remote add -f B https://github.com/korya/B
git checkout -b merge-B-into-b B/master

# Fix the commits
B_SUBDIR=b/
git filter-branch --index-filter \
 'git ls-files -s | sed "s@\t\"*@&'${B_SUBDIR}'@" | \
   GIT_INDEX_FILE=$GIT_INDEX_FILE.new git update-index --index-info && \
   mv "$GIT_INDEX_FILE.new" "$GIT_INDEX_FILE"'
 HEAD

# Merge it to a master
git checkout master
git merge merge-B-into-b

Notes

The only change from the original solution posted in [1] is in --index-filter's sed expression: I've changed the separator from - to @. I'm not sure why, but my sed did not like the - separator.

I'm using GNU sed coming in homebrew:

$ brew --version
0.9.5
$ brew info gnu-sed
gnu-sed: stable 4.2.2 (bottled)
https://www.gnu.org/software/sed/
/usr/local/Cellar/gnu-sed/4.2.2 (9 files, 440K) *
  Built from source with: --default-names
From: https://github.com/Homebrew/homebrew/blob/master/Library/Formula/gnu-sed.rb
==> Options
--with-default-names
	Do not prepend 'g' to the binary
==> Caveats
The command has been installed with the prefix "g".
If you do not want the prefix, install using the "with-default-names" option.

If you need to use these commands with their normal names, you
can add a "gnubin" directory to your PATH from your bashrc like:

    PATH="/usr/local/opt/gnu-sed/libexec/gnubin:$PATH"

Additionally, you can access their man pages with normal names if you add
the "gnuman" directory to your MANPATH from your bashrc as well:

    MANPATH="/usr/local/opt/gnu-sed/libexec/gnuman:$MANPATH"

$ sed --version
sed (GNU sed) 4.2.2
Copyright (C) 2012 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Jay Fenlason, Tom Lord, Ken Pizzini,
and Paolo Bonzini.
GNU sed home page: <http://www.gnu.org/software/sed/>.
General help using GNU software: <http://www.gnu.org/gethelp/>.
E-mail bug reports to: <[email protected]>.
Be sure to include the word ``sed'' somewhere in the ``Subject:'' field.

References

  1. https://stackoverflow.com/questions/6426247/merge-git-repository-in-subdirectory
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment