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

Are you using a new enough git to have git-subtree(1)? I believe that git subtree add -P Cats git://github.com/yournamehere/Cats.git master does everything you want given my quick test (replace Steps 3 & 4 with that single command).

@vmbrasseur
Copy link
Author

Not looking like it, @jhelwig:

trimalchio:~ brasseur$ man git-subtree
No manual entry for git-subtree
trimalchio:~ brasseur$ git help git-subtree
No manual entry for git-subtree
trimalchio:~ brasseur$ git --version
git version 1.7.7

It looks like subtree was added in git v1.7.11. The latest version in homebrew is 1.7.9, but brew is saying that it has nothing to do with the git that's installed on this machine. Frankly, it's been so damn long since I installed it that I have no clue anymore how it got there, which is going to make a "correct" upgrade a little more tricky.

Regardless, for now it looks like I need to do things the hard way (once I can figure out what that is). A pity. It sounded like subtree would've been just what the doctor ordered.

@vmbrasseur
Copy link
Author

Strike that! It looks like I got it from git-scm.com. New .dmg installed. Now:

trimalchio:~ brasseur$ git --version
git version 1.8.1

However...

trimalchio:~ brasseur$ git subtree add foo
git: 'subtree' is not a git command. See 'git --help'.

So now I'm confused again.

@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