Create new git repo from already existing repo's subdirectory

前端 未结 4 591
我在风中等你
我在风中等你 2021-01-02 03:38

I want to create a separate repo from an already existing repo\'s subfolder.

Detach (move) subdirectory into separate Git repository shows exactly that. BUT, I can\'

相关标签:
4条回答
  • 2021-01-02 03:44

    You have two problems here:

    (1) As Kevin Ballard points out, you need to delete the refs/original directory in your .git directory to get rid of the spurious log entries; IIRC this is mentioned in the question you referred to.

    (2) You have to convert the branches one at a time. As far as I know, this isn't mentioned anywhere, but it's pretty easy to discover empirically. A script to do this would look something like this:

    for branch in $(git for-each-ref --format='%(refname:short)' refs/remotes/origin | grep -v HEAD); do
      git checkout -b $(basename $branch) $branch
      git filter-branch -f --subdirectory-filter subdirectory HEAD -- --all
    done
    

    Note that you need the -f or else something like --original $(basename $branch)-original to force git to reuse or rename the namespace where it stores the original refs. Also note that you may see messages like "Found nothing to rewrite" if subdirectory does not exist on a particular branch -- you will probably want to delete those branches from your new repository. (Or delete them before running the script.)

    0 讨论(0)
  • 2021-01-02 03:46

    git filter-branch saves all the original refs into their own namespace (under original/). It sounds like your git log --all is showing those refs too. You should instead inspect all the refs you care about, and if they look good, then you can throw away the original/ namespace.

    0 讨论(0)
  • 2021-01-02 03:55

    Github has a straight forward method:

    https://help.github.com/articles/splitting-a-subpath-out-into-a-new-repository

    git clone git://github.com/defunkt/github-gem.git

    cd github-gem/ Change directory into the repository

    git filter-branch --prune-empty --subdirectory-filter lib master

    0 讨论(0)
  • 2021-01-02 03:56

    I think you need to remove your remotes. e.g.

    git remote rm origin
    

    I thought the same thing you did when I ran git log --all and gitk --all and saw all the commits. Then I realized the extra commits were from the remote.

    0 讨论(0)
提交回复
热议问题