git 如何从现有存储库中的分支创建新的 GitHub 存储库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9527999/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I create a new GitHub repo from a branch in an existing repo?
提问by Dogweather
I have masterand new-projectbranches. And now I'd like to create a brand new repo with its master based on the new-project branch.
我有master和new-project分支。现在我想基于 new-project 分支用它的 master 创建一个全新的 repo。
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.
背景:我有一个包含三个独立应用程序的存储库。它不是这样开始的。存储库中最初只有一个应用程序。然而,随着时间的推移,业务需求发生了变化。一个应用程序变成了两个(旧版本和重写版本)。添加了一个 Web 服务。单独的分支用于包含三个项目。但是,它们不共享任何代码。因此,将它们拆分为自己的存储库会更简单。
回答by Dogweather
I started with @user292677's idea, and refined it to solve my problem:
我从@user292677 的想法开始,并对其进行改进以解决我的问题:
- Create the new-repoin github.
- cd to your local copy of the old repo you want to extract from, which is set up to track the new-projectbranch that will become the new-repo's master.
$ git push https://github.com/accountname/new-repo.git+new-project:master
- 在 github 中创建new-repo。
- cd 到您要从中提取的旧 repo 的本地副本,该副本设置为跟踪将成为new-repo主节点的new-project分支。
$ git push https://github.com/accountname/new-repo.git+new-project:master
The new Github repo is finished. The result is;
新的 Github 存储库已完成。结果是;
- a new Github repository named new-repo,
- whose
master
corresponds to the old repo's new-project, with - all history preserved.
- 一个名为new-repo的新 Github 存储库,
- 其
master
对应于旧 repo 的new-project,与 - 所有的历史都保存了下来。
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 [email protected]:accountname/new_repo +new-project:master +site3a:rails3
$ git push [email protected]:accountname/new_repo +new-project:master +site3a:rails3
The result is that the pre-existing site3abranch is now also moved to the new repo and will appear as rails3. This works really well: the network diagram shows the new masterand rails3with full history and in their correct relationship to each other.
结果是,先前存在的site3a分支现在也移动到了新的存储库,并将显示为rails3。这非常有效:网络图显示了新的master和rails3的完整历史记录以及它们之间的正确关系。
Update 2013-12-07: Used this with another project, and verified that this recipe still works.
2013 年 12 月 7 日更新:将此用于另一个项目,并验证此配方仍然有效。
Update 2018-01-11: Updated step 3. to use GitHub recommendation for https protocol. Recipe still works.
2018 年 1 月 11 日更新:更新了第 3 步,使用 GitHub 推荐的 https 协议。配方仍然有效。
回答by Alexey Kislitsin
Update:
更新:
cd to local repo containing old_branch and:
cd 到包含 old_branch 的本地 repo 和:
$ git push https://github.com/accountname/new_repo.git +old_branch:master
回答by J. C. Salomon
git clone -b new-project /path/to/repo /new/repo/path
Edit: Within GitHub, you can “fork” the repo, then go to the Admin tab in your clone. Beneath “Repository name” and “Visibility” is “Default Branch” with a drop-down menu of branches. Choose new-project
.
编辑:在 GitHub 中,您可以“分叉”存储库,然后转到克隆中的“管理”选项卡。在“存储库名称”和“可见性”下方是带有分支下拉菜单的“默认分支”。选择new-project
。
Re-edit: I just realized it's the master
branch you want set, not just the “default” branch. So…
重新编辑:我刚刚意识到这是master
您要设置的分支,而不仅仅是“默认”分支。所以…
- On GitHub, clone
them/repo
toyou/repo
. - Run
git clone [email protected]:you/repo.git
- Start
gitk
. - [You might want to create an
old-master
branch so you don't lose track of the old commits.] - Find most recent commit on the
new-project
branch, right-click on the commit message, and select “Reset master branch to here”. (You can also do this at the command line usinggit-reset
, but I haven't figured out the correct invocation.)
- 在 GitHub 上,克隆
them/repo
到you/repo
. - 跑
git clone [email protected]:you/repo.git
- 开始
gitk
。 - [你可能想创建一个
old-master
分支,这样你就不会忘记旧的提交。] - 在
new-project
分支上找到最近的提交,右键单击提交消息,然后选择“将主分支重置到此处”。(您也可以在命令行中使用 来执行此操作git-reset
,但我还没有找到正确的调用方式。)
Your next push up to your GitHub repo will need to be done with the --force
option, but otherwise you're done.
您下一次推送到您的 GitHub 存储库将需要使用该--force
选项完成,否则您就完成了。
If it's one of your own repos you're doing this to…
如果它是您自己的仓库之一,那么您这样做是为了……
- Run
git clone [email protected]:you/orig.git
- Run
git clone orig copy
- As I described above, but from within the local
copy
repo, reset themaster
branch to where you want it. - Create the empty GitHub project
you/copy
. Follow the directions on GitHub to set up that project as a remote for your local version ofcopy
, pushmaster
, and you're done!
- 跑
git clone [email protected]:you/orig.git
- 跑
git clone orig copy
- 如上所述,但从本地存储
copy
库中,将master
分支重置到您想要的位置。 - 创建空的 GitHub 项目
you/copy
。按照 GitHub 上的说明将该项目设置为本地版本的远程copy
, pushmaster
,您就完成了!
回答by Joe
- Create the NEW_REPOSITORY in github.
- cd OLD_REPOSITORY
- git push https://github.com/accountname/NEW_REPO+master:master
- 在 github 中创建 NEW_REPOSITORY。
- cd OLD_REPOSITORY
- git push https://github.com/accountname/NEW_REPO+master:master
And that is all. (Note: git history preserved)
这就是全部。(注意:git历史保留)
I had tried the answer above and found it not specific enough as it didn't specify +master:master which is what I needed to make it work. It works great.
我已经尝试了上面的答案,但发现它不够具体,因为它没有指定 +master:master 这是我需要让它工作的东西。它工作得很好。
Source (with my modifications to avoid ssh issues with github): Mauricio Aiello, former Java Senior Developer, https://www.quora.com/How-do-I-create-a-new-GitHub-repository-from-a-branch-in-an-existing-repository
来源(经过我的修改以避免 github 的 ssh 问题):Mauricio Aiello,前 Java 高级开发人员,https://www.quora.com/How-do-I-create-a-new-GitHub-repository-from-a - 现有存储库中的分支
回答by Kjuly
Not sure whether this is a good way, but it's easy anyway:
不确定这是否是一个好方法,但无论如何都很容易:
git clone -b new-project [email protected]:User/YourProject.git newProjcet
Then create a new repo on github, and push it.
然后在github上创建一个新的repo,并推送它。
回答by Julio Flores
Remembering that when you simply create a new repo, you lose reference to the old one, and make it harder to maintain any update to the original project synched to the new one. Perhaps isn't it better to fork the repo?
请记住,当您简单地创建一个新的 repo 时,您将失去对旧 repo 的引用,并且更难维护对与新项目同步的原始项目的任何更新。也许分叉回购不是更好吗?
回答by Gerd
Little addition to the correct answer:
对正确答案的补充:
$ git push [email protected]:accountname/new_repo +old_branch:master
$ git push [email protected]:accountname/new_repo +old_branch:master
"[email protected]:accountname/new_repo" => get from github "Clone or download" pulldown menu
"[email protected]:accountname/new_repo" => 从 github 获取"克隆或下载"下拉菜单