Skip to content

Instantly share code, notes, and snippets.

@vmbrasseur
Last active December 11, 2015 12:18
Show Gist options
  • Save vmbrasseur/4600063 to your computer and use it in GitHub Desktop.
Save vmbrasseur/4600063 to your computer and use it in GitHub Desktop.
A repo is a subdirectory but you'd rather the repo were the parent directory. How do you do that?
I need a git power user to confirm that the steps below are correct before I pull the trigger and risk
completely hosing my repo...
I'm referencing this github helpdoc: https://help.github.com/articles/working-with-subtree-merge
Assume a directory structure of (note: directory names have been changed to protect the innocent):
Animals/
Apes/
Cats/
.git/
Dogs/
Rodents/
Long ago you created a github repo for Cats. Now you'd rather you had the repo on Animals to include
all subdirectories, not just Cats. You'd like to move the Cats repo and history into Animals. So, in
the directory view, you'd rather the above were this:
Animals/
.git/
Apes/
Cats/
Dogs/
Rodents/
How do you do this while still maintaining the history of the Cats repo?
Step 1: Get Cats out of the way
$ cd Animals
$ mv Cats ..
Step 2: Create a repo in Animals
$ cd Animals
$ git init
$ touch .gitignore
$ git add .gitignore
$ git commit -m "Adding .gitignore to new Animals repo"
Step 3: Clone Cats
$ git remote add -f Cats git://github.com/yournamehere/Cats.git
Step 4: Merge Cats with Animals
$ git merge -s ours --no-commit Cats/master # check what merge will do before committing
$ git read-tree --prefix=Cats/ -u Cats/master # read the Cats tree info into the index
$ git commit -m "MOAR CATS" # commit them kitties to the Animal repo
Then add the Apes, Dogs and Rodents subdirs to the new Animals repo. This is standard directory adding
stuff and requires no special handling.
Once Cats is merged into Animals, you can delete the old Cats repo.
@jhelwig
Copy link

jhelwig commented Jan 23, 2013

Well, if you're going to do it the hard way... yeah, that looks like it'll do what you want. You really should look into getting a recent Git on that machine, though. Tons of really nice improvements since then. Try to get something in the 1.8.x series (1.7.7 is from September 2011). I use macports for my stuff instead of homebrew

@jhelwig
Copy link

jhelwig commented Jan 23, 2013

Ah, ha! git-subtree is a contrib script that apparently macports installs by default.

@jhelwig
Copy link

jhelwig commented Jan 23, 2013

@kbaird
Copy link

kbaird commented Jan 23, 2013

A quick & dirty, stone knives & bearskins approach:

  1. Change the Cats repo's name to Animals, locally, on remote host, etc.
  2. mkdir Cats
  3. git mv foo Cats/
  4. commit
  5. add Apes, Rodents, etc.
  6. profit?

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