Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
638 views
in Technique[技术] by (71.8m points)

git - How do I create a new GitHub repo from a branch in an existing repo?

I have master and new-project branches. And now I'd like to create a brand new repo with its master based on the new-project branch.

Background: I have one repository which contains three independent applications. It didn't start out this way. There was originally just one app in the repo. Over time, however, business needs have changed. One app became two (a legacy version and a re-write.) A web service was added. Separate branches were used to contain the three projects. However, they don't share any code. And so it'd be simpler to have them split out into their own repos.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I started with @user292677's idea, and refined it to solve my problem:

  1. Create the new-repo in github.
  2. cd to your local copy of the old repo you want to extract from, which is set up to track the new-project branch that will become the new-repo's master.
  3. $ git push https://github.com/accountname/new-repo.git +new-project:master

The new Github repo is finished. The result is;

  • a new Github repository named new-repo,
  • whose master corresponds to the old repo's new-project, with
  • all history preserved.

In fact, I found that by using this method, I could create the new repo with a hand-picked selection of branches, renamed as I wanted:

$ git push git@github.com:accountname/new_repo +new-project:master +site3a:rails3

The result is that the pre-existing site3a branch is now also moved to the new repo and will appear as rails3. This works really well: the network diagram shows the new master and rails3 with full history and in their correct relationship to each other.

Update 2013-12-07: Used this with another project, and verified that this recipe still works.

Update 2018-01-11: Updated step 3. to use GitHub recommendation for https protocol. Recipe still works.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...