Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
660 views
in Technique[技术] by (71.8m points)

how to create a new git repository from an existing one

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...