Git - How to split a branch to it's own repository?

空扰寡人 提交于 2021-01-29 03:44:58

问题


I have a repository where a branch (admin) has been branched out from develop at some point. It looks roughly like this

develop            c2        c4        c5
      *------------*---------*---------*------------...
      c1            \ 
                     \____________________*c3
                          admin

I want the develop branch to stay the same and admin branch should have it's own repository. For the record, I'm using sourcetree+bitbucket.

I tried the method mentioned in this answer and tried git push url://to/new/repository.git admin:develop. What it did in my new repo is that the history started from the first commit of develop (c1). it looks like this in the new repository.

c1      c2                           c3
*-------*----------------------------*
             develop

I wanted to start this new repository from c2 however. Is it normal behavior or have I done somthing wrong? What's the correct way to achieve desired result?


回答1:


git checkout -b sliced-admin admin                   # start with admin
git rev-list develop.. | tail -1 >.git/info/grafts   # slice out the develop history
git filter-branch                                    # .
rm .git/info/grafts                                  # .

Now push the sliced-admin branch wherever you want

git push u://r/l sliced-admin:itsnamethere



回答2:


Because c1 is the parent of c2 then there is no way to accomplish this without rewriting history and changing the commit hashes. But since you're creating a new repository anyway, then this might be acceptable and so you've got a couple options.

Consider this history:

* cd857c2 (HEAD -> develop) c5
* eda39a7 c4
| * 49671f2 (admin) c3
|/  
* 53b169b c2
* 42bf35f c1

I would suggest creating an orphan branch from c2

git checkout --orphan newDev 53b169b

This will create a branch with a new root unrelated to the history of the rest of your repository, and stage all the files in the repository at that point in time to the index, so now you can modify that index as you see fit with reset and then make the initial commit.

git commit -m "c1/c2"

Lastly, cherry-pick the range of commits from the starting commit to the admin branch tip.

git cherry-pick 53b169b..admin

Now you've got an all new branch with completely different commit hashes for the same content starting at your desired point.

* d19cdd1 (HEAD -> newDev) c3
* f886282 c1/c2

Finally push that new branch.

git push url://to/new/repository.git newDev:develop


来源:https://stackoverflow.com/questions/40340194/git-how-to-split-a-branch-to-its-own-repository

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!