105

I have a remote git repository that really replaced everything we had in another older SCM. Many projects and products have been added to the repository over the years.

There is a branch in this repo, corresponding to a product that I am interested in. I want to make a brand new git repository from this branch only, not really concerned about loss of history.

Is git remote add the solution? I want for both of these repositories to be on the same server.

Thoughts?

5 Answers 5

108

In order to create a new Git repository from an existing repository one would typically create a new bare repository and push one or more branches from the existing to the new repository.

The following steps illustrates this:

  1. Create a new repository. It must be bare in order for you to push to it.

    $ mkdir /path/to/new_repo
    $ cd /path/to/new_repo
    $ git --bare init

    Note: ensure that your new repository is accessible from the existing repository. There are many ways to do this; let's assume that you have made it accessible via ssh://my_host/new_repo.

  2. Push a branch from your existing repository. For example let's say we want to push the branch topic1 from the existing repository and name it master in the new repository.

    $ cd /path/to/existing_repo
    $ git push ssh://my_host/new_repo +topic1:master

This technique allows you to keep the history from the existing branch.

Note: the new repository is effectively a new remote repository. If you want to work with the new repository you must clone it. The following will clone the new repo into a local working directory called new_repo:

$ git clone ssh://my_host/new_repo

In this example, when you clone the new repository you will see that the master branch is a copy of the topic1 branch of the old repository.

4
  • 12
    If you will be pushing regularily to the remote repo, it's probably a good advice to git remote add origin ssh://my_host/new_repo. This way you' just do a git push origin [branchname] . When using UNC filepathes (for windowsshares for example), be sure to enter add the remote origin for them like this: git remote add origin "//server_name/myapp/" Commented Sep 12, 2014 at 10:15
  • Can the new repository be accessible as a local directory/repository? Are there any update methods to do this? Commented Aug 21, 2018 at 9:17
  • @NikosAlexandris I've added new information, hope it helps. Also note that it doesn't matter that the new repo is on a remote server. In the example we push and clone using the ssh protocol; but we could similarly have used the file protocol which would have created the new repository on the same file system.
    – pestrella
    Commented Aug 21, 2018 at 12:12
  • Had to use -f to force push. Anyway this solution worked. Thanks a lot.
    – Kasun
    Commented Apr 29, 2021 at 4:43
28

If you're not worried about losing history, do a git checkout mybranch and then copy the directory contents to another folder. Within that folder, delete the .git folder and then:

git init; git commit -a -m "Imported from project Y"
3
  • 2
    will this keep the commit history of the original repo?
    – Mike Graf
    Commented Jan 30, 2013 at 20:18
  • 24
    @MikeGraf: No, as pointed out in the first part of the first sentence in the answer: "If you're not worried about losing history..." Commented Jan 30, 2013 at 23:07
  • 2
    You might have to remove/edit .gitignore as well, otherwise the above command will leave behind potentially important files during the commit.
    – Dave
    Commented Jun 8, 2017 at 18:23
27

Pull down the branch like normal and then push the branch to a new repository that you have created using git init. You would use code that looks something like:

git push url:///new/repo.git TheBranchFolder

This method also keeps all of your previous changes if that is a plus for the situation.

5
  • I am using ssh access to my repo, can I use that instead of the URL in your response?
    – reza
    Commented Mar 23, 2012 at 18:06
  • reza: yes, think of the ssh URL as just a different type of URL.
    – amcnabb
    Commented Mar 23, 2012 at 18:16
  • so on the git server, I have issued git push /home/git/newBranchGIT branchname.... Now what I see is a git repo that has everything I need but the only branch is now called branchname and not origin or master. I want the branchname to be master. Or is there a better way of doing this?
    – reza
    Commented Mar 23, 2012 at 21:08
  • Just change the name as you wish and commit. Commented Mar 25, 2012 at 15:53
  • If you have created a brand new repository in Git Hub and have not done anything else to it yet and have the default view suggesting git commands to get started. Then you can use the above command without specifying 'TheBranchFolder'. e.g. 'git push url:///new/repo.git'
    – phildn
    Commented Aug 20, 2023 at 9:25
9

I don't know my answer still helps or not but this is another way to create a new repository using an existing one.
step 1: clone the existing repo.
step 2: delete the .git folder in cloned repo folder
step 3: create a new repo in git.
step 4: in the cloned repo folder run git init
step 5: git add .
step 6: git remote add origin NEW_REPO/URL
step 7: git branch -M "branch_name"
step 8: git push --set-upstream origin branch_name

1

Our computing environment requires that git repos are created on a remote server, so git init is not a good option. I did this instead.

git clone <NEW, EMPTY REPO> new
git clone <EXISTING REPO> old
cd new
git remote add --no-tags -f old_repo ../old
git checkout -b main old_repo/main
git push origin main

Not the answer you're looking for? Browse other questions tagged or ask your own question.