gitversion-controlreorganize

git - Move Subdirectories into Branches


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.


Solution

  • Here's a moderately concise approach:

    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"