2

I have a git repo with several subdirectories in its root, for example

repo<master>/a/files
repo<master>/b/files
...

I would like to move each subdirectory out of the master branch and into it's own branch, and move the files up one directory in the process, so that I'm left with something like this.

repo<branch_a>/files
repo<branch_b>/files
...

I have a few ideas of how to do it, for example:

  1. Deleting all items from the master, creating a new branch, copying the files I need for that branch, committing. The problem with this is that I'll lose the commit history of the files.
  2. Branching, deleting directories I don't need, moving files up one level, committing. This will maintain history but I'm to have a lot of identical deletes in every branch.

Is there anyway I can do this more efficiently, i.e. less deletes but still maintain revision history?

This question is similar to Detach (move) subdirectory into separate Git repository but instead of moving each subdirectory into a separate repository I want to move it into its own branch.

Community
  • 1
  • 1
Samuel
  • 8,063
  • 8
  • 45
  • 41

2 Answers2

3

Here's a moderately concise approach:

  • Delete all subdirectories on master, each in an individual commit
  • Create each branch you want, based from master
  • On each branch, revert the commit that deleted the directory the branch wants
  • Move the directory up a level

Pseudobash:

$ for dir in one two three; do git rm -r $dir; git commit -m "Remove $dir"; done
$ git log # For commit SHAs
$ git checkout -b one
$ git revert $sha_that_removed_one
$ git mv one/* .
$ git commit -m "Move one files to the top level"
Kristján
  • 18,165
  • 5
  • 50
  • 62
  • This sounds like a viable solution. How would I do "On each branch, revert the commit that deleted the directory the branch wants" with git? I don't understand what you mean by "You could do all the moves first, too, if you don't mind your branches having extraneous moves before the deletes." – Samuel May 15 '15 at 19:30
  • 2
    Updated with a bashy example - is that more helpful? With some more effort, you could automate the branch/revert/move logic per directory. Nevermind the part about moving first, since it looks like you only have one level of directories, and all their files would get muddled together. – Kristján May 15 '15 at 21:27
  • 2
    This worked perfect. Clever :) One thing that cost me some time, git revert uses a default commit message. My git maintainer requires commit messages to have a specific format, and has a hook in place that prevents incorrectly formatted messages from being pushed. So after I thought I was all done I had to go through and manually rebase/reword several commits. The commit message can be edited with the git revert's --edit flag. Thanks! – Samuel May 21 '15 at 16:19
  • Nice tip, `--edit` sounds like a great way to add context too. Thanks! – Kristján May 21 '15 at 16:25
0

This seems to be a good place to use git submodules

Nick Volynkin
  • 14,023
  • 6
  • 43
  • 67
  • I read the first few paragraphs in the link you provided. I don't see how this is related, git submodules are essentially nested git repositories. This is not what I'm looking for and doesn't fit my needs. – Samuel May 19 '15 at 13:23