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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9844082/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 13:18:19  来源:igfitidea点击:

how to create a new git repository from an existing one

git

提问by reza

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.

我有一个远程 git 存储库,它真正取代了我们在另一个旧 SCM 中的所有内容。多年来,许多项目和产品已添加到存储库中。

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.

这个repo里面有一个分支,对应我感兴趣的一个产品。我只想从这个分支做一个全新的git仓库,不是很关心历史的丢失。

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

git remote 是否添加了解决方案?我希望这两个存储库位于同一台服务器上。

Thoughts?

想法?

回答by pestrella

In order to create a new Git repository from an existing repository one would typically create a new barerepository 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 barein 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 topic1from the existing repository and name it masterin the new repository.

    $ cd /path/to/existing_repo
    $ git push ssh://my_host/new_repo +topic1:master
  1. 创建一个新的存储库。它必须是裸露的,这样你才能推动它。

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

    注意:确保可以从现有存储库访问您的新存储库。有很多方法可以做到这一点;假设您已通过ssh://my_host/new_repo.

  2. 从现有存储库推送分支。例如,假设我们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 remoterepository. 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 masterbranch is a copy of the topic1branch of the old repository.

在此示例中,当您克隆新存储库时,您将看到该master分支是topic1旧存储库分支的副本。

回答by JustinDoesWork

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 init. 您将使用类似于以下内容的代码:

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

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

如果这对这种情况有利,此方法还会保留您之前的所有更改。

回答by Justin ??????

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

如果您不担心丢失历史记录,请执行 agit checkout mybranch然后将目录内容复制到另一个文件夹。在该文件夹中,删除 .git 文件夹,然后:

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