问题
I have created a new project locally and it is a template application that has some base code (for upcoming projects), and I pushed it to a remote repository on Github. And all is working fine.
I am trying to create new projects (with their own local and remote repositories) out of that template app. For example, say the current project is called TemplateApp. I want to build a new project (based on TemplateApp) but is called CoffeeMaker (name should both be locally called and remotely). How can I do that?
I tried cloning TemplateApp, then renaming the project locally (and all its files - which is FRUSTRATING) and then do git init
and finally push to a new repository. IF this works, I have not succeeded yet as I kept failing in renaming the project locally.
回答1:
- Create a new remote repository CoffeeMaker on Github.
- In the local TemplateApp, create a new branch which will be the main branch of CoffeeMaker.
- Push the new branch to the remote CoffeeMaker.
When creating the new branch, you could choose to preserve the previous history or not. Suppose to create new
from the latest master
in TemplateApp:
#enter the local TemplateApp
cd TemplateApp
#preserve the history
git checkout -b new master
#don't preserve the history
git checkout --orphan new master
git commit -m 'init'
#modify the files
git add .
git commit -m 'changes for CoffeeMaker'
#push the new branch to TemplateApp
git push https://github.com/xxx/CoffeeMaker.git -f new:refs/heads/master
#or
git remote add coffee https://github.com/xxx/CoffeeMaker.git
git push coffee -f new:refs/heads/master
Now the new repository is created with a master
branch. You and others can now clone and work:
git clone https://github.com/xxx/CoffeeMaker.git
回答2:
On GitHub, since June 2019, you don't have to clone "TemplateApp
" and rename it.
You now can define a repository template, and call the /generate
endpoint (or click on the button "Use this template").
All you will need to do is name your project and clone your new repository to get started, based on that one "TemplateApp
" repository template!
See "Generate new repositories with repository templates":
Sharing boilerplate code across codebases is a constant pattern in software development.
Bootstrapping a new project with our favorite tools and directory structures helps programmers go from idea to “Hello world!” more efficiently and with less manual configuration.Today, we’re excited to introduce repository templates to make boilerplate code management and distribution a first-class citizen on GitHub.
To get started, all you need to do is mark a repository as a template, and you’ll immediately be able to use it to generate new repositories with all of the template repository’s files and folders.
Every template repository gets a new URL endpoint called
/generate
that allows you to distribute your template more efficiently
来源:https://stackoverflow.com/questions/48958546/can-i-create-a-new-repository-out-of-an-existing-repository-but-rename-it