Skip to content

Instantly share code, notes, and snippets.

@vindir
Forked from bxt/merge_git_repos.md
Created January 29, 2018 18:26
Show Gist options
  • Save vindir/a194e1e6ba5277ad098ba4fb6f1d1d58 to your computer and use it in GitHub Desktop.
Save vindir/a194e1e6ba5277ad098ba4fb6f1d1d58 to your computer and use it in GitHub Desktop.
Git: Merge other repository into subfolder

Copy the child repo to your /tmp (Or into another ramdisk, should be faster)

cd into the new repo.

Now lets rewirte stuff.

Move into subdirectory (here called "report"):

git filter-branch --index-filter \
      'git ls-files -s | sed "s-$(printf '\''\t'\'')\"*-&report/-" | 
          GIT_INDEX_FILE=$GIT_INDEX_FILE.new git update-index --index-info && 
      mv $GIT_INDEX_FILE.new $GIT_INDEX_FILE' HEAD

Not check your working directory. There may be som gitignored files. You'll probably want to keep some of those somewhere.

You might want to check the new refs with gitk

It can be a neat idea to prefix them commits:

git filter-branch -f --msg-filter 'sed "1s/^/T: /"'

Tada! You have moved all your stuff into a subdirectory and prefixed the commits with "T: "

Its time to merge your stuff into your parent repo. Easy as:

git pull /tmp/report-repo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment