简体   繁体   English

如何从现有的git存储库创建一个新的git存储库

[英]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. 我有一个远程git存储库,它真正取代了我们在另一个旧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. 这个仓库中有一个分支,对应于我感兴趣的产品。我想只从这个分支创建一个全新的git存储库,而不是真正关注历史的丢失。

Is git remote add the solution? 是git remote添加解决方案吗? I want for both of these repositories to be on the same server. 我希望这两个存储库都在同一台服务器上。

Thoughts? 思考?

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. 为了从现有存储库创建新的Git存储库,通常会创建一个新的存储库并将一个或多个分支从现有存储库推送到新存储库。

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 . 我们假设您已通过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. 例如,假设我们要从现有存储库中推送分支topic1 ,并将其命名为新存储库中的master

     $ 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 : 以下内容将新repo克隆到名为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. 在此示例中,当您克隆新存储库时,您将看到master分支是旧存储库的topic1分支的副本。

If you're not worried about losing history, do a git checkout mybranch and then copy the directory contents to another folder. 如果您不担心丢失历史记录,请执行git checkout mybranch ,然后将目录内容复制到另一个文件夹。 Within that folder, delete the .git folder and then: 在该文件夹中,删除.git文件夹,然后:

git init; git commit -a -m "Imported from project Y"

Pull down the branch like normal and then push the branch to a new repository that you have created using git init . 像往常一样下拉分支,然后将分支推送到您使用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. 如果这是对情况的加分,此方法还会保留您之前的所有更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM