239

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.

Dogweather
  • 15,512
  • 17
  • 62
  • 81

8 Answers8

427

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.

Dogweather
  • 15,512
  • 17
  • 62
  • 81
  • 1
    Hi @Dogweather, thanks for sharing this. can you explian what's the different between your method and change the origin remote url, push to new repo method? – Vincent May 01 '13 at 14:16
  • 9
    Dogweather, I've used your solution more than I can remember. Thanks! I had to checkout old_branch first before this worked for me – Bjorn Theart Jan 22 '15 at 13:21
  • 5
    note that this won't copy tags. I believe you may need `--follow-tags` for that. – Factor Mystic Aug 31 '15 at 22:28
  • to clarify, your `old_branch` in this answer corresponds to `new-project` in your original question? – colin Dec 10 '15 at 14:32
  • I did this too and got the new repository :-) But when cloning that to the local computer it was always empty and github desktop could not sync, it instead had the button to publish :-( – Alex Mar 07 '16 at 09:19
  • 1
    Note that you don't need to create a repo on Github, you can just `push` to a local one (i.e. `git init` instead of Github > New > ...) – OJFord Apr 19 '16 at 12:19
  • If you have autentication issues use https: `git push https://github.com/accountname/new-repo +new-project:master` – Joseph Garrone Jul 30 '17 at 05:19
  • 1
    I don`t want to transfer all my commit history to new repo, i just wanted to copy commits which belongs to the child repository. Is this possible ?? – Arbaz Ali Rizvi Nov 14 '17 at 05:56
  • @ArbazRizvi I agree with you here, doing it this way, also takes all the history and commits of all the files, which were in fact removed in this specific branch (in this case new-project) to the new repok which in turn increases repo size. Did you get a solution to this. – whyAto8 Jun 27 '19 at 06:47
  • 3
    To be clear, use this on Git bash: ```git push [SSH address of repo for clone] +[old repo branch name]:[new repo branch name]```. If you're copying the whole repo, old and new repo branch name will likely to be **master**. – tkpb Jun 04 '20 at 16:28
  • As mentioned no doubt countless other places, Github's default branch is now "main" instead of "master". – Jeff Lowery Dec 24 '21 at 00:23
  • 1
    +1 I've just used this for a project stored in the GitLab repo and it worked perfectly! In my case I was supposed to make a new-repo with one branch develop based on branch old-branch from old-repo, so I've created a new-project in the GitLab UI and pushed changes to new-repo with the command: `git push https://path/to/new-project.git +old-branch:develop` – kaspiotr Apr 26 '22 at 16:15
41

cd to local repo containing old_branch and:

$ git push https://github.com/accountname/new_repo.git +old_branch:master
desertnaut
  • 57,590
  • 26
  • 140
  • 166
Alexey Kislitsin
  • 726
  • 7
  • 12
12
  1. Create the NEW_REPOSITORY in github.
  2. cd OLD_REPOSITORY
  3. git push https://github.com/accountname/NEW_REPO +master:master

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

Joe
  • 965
  • 11
  • 16
  • 3
    That’s actually more usable than the accepted answer. It works even in situations where Git complains about src refspec errors when failing to push the old to the new repository. – Informagic Jun 14 '19 at 16:02
  • 1
    worth noting that this is pushing master into master, if you want to push a specific branch do ...+{old_branch_name}:{new_branch_name} – Mojimi Apr 08 '21 at 21:01
9
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…

  • On GitHub, clone them/repo to you/repo.
  • Run git clone git@github.com: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 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…

  • Run git clone git@github.com:you/orig.git
  • Run git clone orig copy
  • As I described above, but from within the local copy repo, reset the master 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 of copy, push master, and you’re done!
desertnaut
  • 57,590
  • 26
  • 140
  • 166
J. C. Salomon
  • 4,143
  • 2
  • 29
  • 38
2

Not sure whether this is a good way, but it's easy anyway:

git clone -b new-project git@github.com:User/YourProject.git newProjcet

Then create a new repo on github, and push it.

Kjuly
  • 34,476
  • 22
  • 104
  • 118
1

If you want other branch instead of main, just do this,

  1. Create new-repo on Github
  2. CD to the old repo (In my case, I am at the old repo and the branch I want to copy)
  3. git push https://github.com/your_username/new-repo.git +branch_from_the_old_repo:the_main_or_whichever_branch_from_the_new_repo

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))

HilthPH
  • 25
  • 4
0

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?

Julio Flores
  • 435
  • 4
  • 7
0

If you are in love with SourceTree just do some simple step

  1. Check out the brand you want to clone as new repo

  2. Open terminal on this brand by click on the terminal button on toolbar in SourceTree

  3. 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.

desertnaut
  • 57,590
  • 26
  • 140
  • 166