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.

2 Answers 2

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" 
Sign up to request clarification or add additional context in comments.

4 Comments

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."
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.
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!
Nice tip, --edit sounds like a great way to add context too. Thanks!
-1

This seems to be a good place to use git submodules

1 Comment

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.