将 Git 存储库内容移动到另一个存储库以保留历史记录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17371150/
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 16:27:30  来源:igfitidea点击:

Moving Git repository content to another repository preserving history

gitmergerepositorymigrate

提问by Mario

I am trying to move only the contents of one repository (repo1) to another existing repository (repo2) using the following commands:

我正在尝试使用以下命令仅将一个存储库 ( repo1)的内容移动到另一个现有存储库 ( repo2):

git clone repo1
git clone repo2
cd repo1
git remote rm origin
git remote add repo1
git push

But it's not working. I reviewed a similar post, but I only found one moving the folder, not the contents.

但它不起作用。我查看了一个类似的帖子,但我只发现一个移动文件夹,而不是内容。

回答by Chronial

I think the commands you are looking for are:

我认为您正在寻找的命令是:

cd repo2
git checkout master
git remote add r1remote **url-of-repo1**
git fetch r1remote
git merge r1remote/master --allow-unrelated-histories
git remote rm r1remote

After that repo2/masterwill contain everything from repo2/masterand repo1/master, and will also have the history of both of them.

之后repo2/master将包含来自repo2/master和 的所有内容repo1/master,并且还将拥有两者的历史记录。

回答by Sergey Onishchenko

Perfectly described here https://www.smashingmagazine.com/2014/05/moving-git-repository-new-server/

这里完美描述https://www.smashingmagazine.com/2014/05/moving-git-repository-new-server/

First, we have to fetch all of the remote branches and tags from the existing repository to our local index:

首先,我们必须从现有存储库中获取所有远程分支和标签到我们的本地索引:

git fetch origin

We can check for any missing branches that we need to create a local copy of:

我们可以检查创建本地副本所需的任何缺少的分支:

git branch -a

Let's use the SSH-cloned URL of our new repository to create a new remote in our existing local repository:

让我们使用新存储库的 SSH 克隆 URL 在我们现有的本地存储库中创建一个新的远程:

git remote add new-origin [email protected]:manakor/manascope.git

Now we are ready to push all local branches and tags to the new remote named new-origin:

现在我们准备将所有本地分支和标签推送到名为 new-origin 的新远程:

git push --all new-origin 
git push --tags new-origin

Let's make new-origin the default remote:

让我们将 new-origin 设为默认遥控器:

git remote rm origin

Rename new-origin to just origin, so that it becomes the default remote:

将 new-origin 重命名为 origin,使其成为默认远程:

git remote rename new-origin origin

回答by Dan Cohn

If you're looking to preserve the existing branches and commit history, here's one way that worked for me.

如果您希望保留现有分支和提交历史记录,这里有一种对我有用的方法。

git clone --mirror https://github.com/account/repo.git cloned-repo
cd cloned-repo
git push --mirror {URL of new (empty) repo}

# at this point only remote cloned-repo is correct, local has auto-generated repo structure with folders such as "branches" or "refs"
cd ..
rm -rf cloned-repo
git clone {URL of new (empty) repo}
# only now will you see the expected user-generated contents in local cloned-repo folder

# note: all non-master branches are avaialable, but git branch will not show them until you git checkout each of them
# to automatically checkout all remote branches use this loop:
for b in `git branch -r | grep -v -- '->'`; do git branch --track ${b##origin/} $b; done

Now, suppose you want to keep the source and destination repos in sync for a period of time. For example, there's still activity within the current remote repo that you want to bring over to the new/replacement repo.

现在,假设您希望在一段时间内保持源和目标存储库同步。例如,您希望将当前远程存储库中的活动转移到新的/替换存储库中。

git clone -o old https://github.com/account/repo.git my-repo
cd my-repo
git remote add new {URL of new repo}

To pull down the latest updates (assuming you have no local changes):

要下拉最新更新(假设您没有本地更改):

git checkout {branch(es) of interest}
git pull old
git push --all new

NB: I have yet to use submodules, so I don't know what additional steps might be required if you have them.

注意:我还没有使用子模块,所以我不知道如果你有它们可能需要哪些额外的步骤。

回答by raddevus

This worked to move my local repo (including history) to my remote github.com repo. After creating the new empty repo at GitHub.com I use the URL in step three below and it works great.

这有助于将我的本地存储库(包括历史记录)移动到我的远程 github.com 存储库。在 GitHub.com 上创建新的空存储库后,我使用下面第三步中的 URL,效果很好。

git clone --mirror <url_of_old_repo>
cd <name_of_old_repo>
git remote add new-origin <url_of_new_repo>
git push new-origin --mirror

I found this at: https://gist.github.com/niksumeiko/8972566

我在以下位置找到了这个:https: //gist.github.com/niksumeiko/8972566

回答by Martin

Simplest approach if the code is already tracked by Git then set new repository as your "origin" to push to.

如果代码已被 Git 跟踪,则最简单的方法是将新存储库设置为要推送到的“源”。

cd existing-project
git remote set-url origin https://clone-url.git
git push -u origin --all
git push origin --tags

回答by DalSoft

As per @Dan-Cohn answer Mirror-push is your friend here. This is my go to for migrating repos:

根据@Dan-Cohn 的回答,Mirror-push 是您的朋友。这是我迁移存储库的方法:

Mirroring a repository

镜像仓库

1.Open Git Bash.

1、打开Git Bash。

2.Create a bare clone of the repository.

2.创建仓库的裸克隆。

$ git clone --bare https://github.com/exampleuser/old-repository.git

3.Mirror-push to the new repository.

3.镜像推送到新的仓库。

$ cd old-repository.git
$ git push --mirror https://github.com/exampleuser/new-repository.git

4.Remove the temporary local repository you created in step 1.

4. 删除您在步骤 1 中创建的临时本地存储库。

$ cd ..
$ rm -rf old-repository.git

Reference and Credit: https://help.github.com/en/articles/duplicating-a-repository

参考和信用:https: //help.github.com/en/articles/duplicating-a-repository

回答by Roopkumar Akubathini

I used the below method to migrate my GIT Stash to GitLab by maintaining all branches and commit history.

我使用以下方法通过维护所有分支和提交历史将我的 GIT Stash 迁移到 GitLab。

Clone the old repository to local.

将旧存储库克隆到本地。

git clone --bare <STASH-URL>

Create an empty repository in GitLab.

在 GitLab 中创建一个空的存储库。

git push --mirror <GitLab-URL>

回答by Eric Palace

It looks like you're close. Assuming that it's not just a typo in your submission, step 3 should be cd repo2instead of repo1. And step 6 should be git pullnot push. Reworked list:

看起来你很接近。假设这不仅仅是您提交的错字,步骤 3 应该cd repo2代替 repo1。并且第 6 步不应git pull推动。重做清单:

1. git clone repo1
2. git clone repo2
3. cd repo2
4. git remote rm origin
5. git remote add repo1
6. git pull
7. git remote rm repo1
8. git remote add newremote

回答by Thomas

There are a lot of complicated answers, here; however, if you are not concerned with branch preservation, all you need to do is reset the remote origin, set the upstream, and push.

这里有很多复杂的答案;但是,如果您不关心分支保留,您需要做的就是重置远程源、设置上游和推送。

This worked to preserve all the commit history for me.

这有助于为我保留所有提交历史记录。

cd <location of local repo.>
git remote set-url origin <url>
git push -u origin master