Skip to content

Instantly share code, notes, and snippets.

@simonw
Created October 31, 2018 18:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simonw/75e3560a12ca3174ee9c97dd311e8636 to your computer and use it in GitHub Desktop.
Save simonw/75e3560a12ca3174ee9c97dd311e8636 to your computer and use it in GitHub Desktop.
How to import a GitHub repository as a subdirectory of a new repository while maintaining commits and datestamps

How to import a GitHub repository as a subdirectory of a new repository while maintaining commits and datestamps

There is probably a better way to do this, but this worked for me.

I had a repository called docsearch that I had been building a prototype in.

I wanted to move the contents of that repository into an existing repository called search_experiments - but I wanted the contents to live in a docsearch/ subdirectory rather than living in the root of the repo.

I solved this using the combination of git format-patch and git apply.

First, I created a list of patch files in my docsearch repo by running this:

cd docsearch
git format-patch --root

The --root option means "start at the first commit in the repo". This produced a list of patch files:

docsearch $ ls -l *.patch 
-rw-r--r--  1 simonw  staff  3910 Oct 31 11:11 0001-First-working-version.patch
-rw-r--r--  1 simonw  staff  6591 Oct 31 11:11 0002-Implemented-faceted-search.patch
-rw-r--r--  1 simonw  staff  1403 Oct 31 11:11 0003-Add-updated_by-facet.patch
-rw-r--r--  1 simonw  staff  1558 Oct 31 11:11 0004-Don-t-show-snippet-if-no-search-term.patch
-rw-r--r--  1 simonw  staff  1211 Oct 31 11:11 0005-Use-searchterm-for-better-autocomplete.patch
-rw-r--r--  1 simonw  staff  1372 Oct 31 11:11 0006-Show-truncated-content-summary-if-no-snippet.patch

I then manually applied these patches to the other repository, one at a time - but I used the git apply --directory=docsearch option to get the patches to create files in a new subdirectory. I did the following for each patch file:

cd ../search_experiments/
git apply --directory=docsearch ../docsearch/0001-First-working-version.patch
# This applies changes but does not commit. Commit them, duplicating the old commit message
git add docsearch
git commit -m "First working version of docsearch" # Manually copied commit message
# Last step: manually edit the date to reflect the old commit
git commit --amend --no-edit --date "Thu, 27 Sep 2018 10:54:02 -0700"

Since I only had 6 patches I didn't bother to investigate further options for automating the commit message and date duplication.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment