Reputation: 16819
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.
Upvotes: 264
Views: 143909
Reputation: 4313
git clone -b new-project /path/to/repo /new/repo/path
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
.
Edit: I just realized it’s the master
branch you want set, not just the “default” branch. So…
them/repo
to you/repo
.git clone [email protected]:you/repo.git
gitk
.old-master
branch so you don’t lose track of the old commits.]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 using git-reset
, but I haven’t figured out the correct invocation.)Your next push up to your GitHub repo will need to be done with the --force
option, but otherwise you’re done.
If it’s one of your own repos you’re doing this to…
git clone [email protected]:you/orig.git
git clone orig copy
copy
repo, reset the master
branch to where you want it.you/copy
. Follow the directions on GitHub to set up that project as a remote for your local version of copy
, push master
, and you’re done!Upvotes: 9
Reputation: 247
If you are in love with SourceTree just do some simple step
Check out the brand you want to clone as new repo
Open terminal on this brand by click on the terminal button on toolbar in SourceTree
Give the command to the terminal $ git push https://github.com/accountname/new_repo.git
The git command based on Alexey Kislitsin's answer above.
Upvotes: 0
Reputation: 806
cd to local repo containing old_branch and:
$ git push https://github.com/accountname/new_repo.git +old_branch:master
Upvotes: 49
Reputation: 35
If you want other branch instead of main, just do this,
I copied a branch from my old repo to my new repo (It was pushed into another branch instead of main, it depends on what you type after the colon (the BOLD part on step 3))
Upvotes: 2
Reputation: 16819
I started with @user292677's idea, and refined it to solve my problem:
$ git push https://github.com/accountname/new-repo.git +new-project:master
The new Github repo is finished. The result is;
master
corresponds to the old repo's new-project, withIn 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
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.
Upvotes: 462
Reputation: 1045
And that is all. (Note: git history preserved)
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.
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
Upvotes: 13
Reputation: 433
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?
Upvotes: 0
Reputation: 35181
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.
Upvotes: 2