0

I have the following repository foo.git with the following sub-folders.

/foo
----/bar1
----/bar2

I want to create a new separate repository bar1.git along with its commit history, with two copies of the contents now - at bar1.git and foo.git/bar1.

Also, what would be a good way to merge back the updated contents of bar1.git with foo.git/bar1 at a later point of time with the new commit history retained?

I am aware of subtree, submodule and filter-branch commands but not sure which one and how exactly to use them in my case.

heethesh
  • 351
  • 1
  • 5
  • 11
  • Possible duplicate of [Detach (move) subdirectory into separate Git repository](https://stackoverflow.com/questions/359424/detach-move-subdirectory-into-separate-git-repository) – phd Jan 08 '19 at 00:08
  • https://stackoverflow.com/search?q=%5Bgit%5D+detach+directory – phd Jan 08 '19 at 00:08

1 Answers1

1

Solution with git-submodule is the following:

  1. Create bar1.git repository
  2. Go to your local foo repository and add git-submodule inside with a command:

    git submodule add -b master https://github.com/user/bar1.git bar1
    
  3. Update git-submodule with a command:

    git submodule update --init --recursive --remote
    

    Use this command after any new clone foo repository as well.

  4. You'll see .gitmodules file with content similar to:

    [submodule "bar1"]
    path = bar1
    url = https://github.com/user/bar1.git
    branch = master
    
  5. Commit and pull your changes in foo repository

  6. If you make any changes to bar1 repository you need to sync it with your foo\bar1 submodule. This can be done with set of commands:

    git pull
    git submodule update --init --recursive --remote
    git pull --recurse-submodules
    git submodule foreach "(git checkout master; git pull)"
    git add --all
    git commit -m "Submodule Sync"
    git push        
    

    This command will recursively update and sync ALL submodules in foo repository to the latest revision in master branch.

chronoxor
  • 3,159
  • 3
  • 20
  • 31