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

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

这个回购协议中有一个分支,与我感兴趣的产品相对应。 我只想从这个分支创建一个全新的 git 存储库,并不真正关心历史的丢失。

是 git 远程添加解决方案吗? 我希望这两个存储库在同一个服务器上。

有什么想法吗?

105633 次浏览

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"

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.

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.

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

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