简体   繁体   English

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

[英]Moving Git repository content to another repository preserving history

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.我查看了一个类似的帖子,但我只发现一个移动文件夹,而不是内容。

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/master will contain everything from repo2/master and repo1/master , and will also have the history of both of them.之后repo2/master将包含来自repo2/masterrepo1/master ,并且还将拥有它们的历史记录。

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 git@github.com: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

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.注意:我还没有使用子模块,所以我不知道如果你有它们可能需要哪些额外的步骤。

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

This worked to move my local repo (including history) to my remote github.com repo.这有助于将我的本地存储库(包括历史记录)移动到我的远程 github.com 存储库。 After creating the new empty repo at GitHub.com I use the URL in step three below and it works great.在 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

As per @Dan-Cohn answer Mirror-push is your friend here.根据@Dan-Cohn 的回答,Mirror-push 是您的朋友。 This is my go to for migrating repos:这是我迁移存储库的方法:

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

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>

It looks like you're close.看起来你很接近。 Assuming that it's not just a typo in your submission, step 3 should be cd repo2 instead of repo1.假设这不仅仅是您提交的拼写错误,步骤 3 应该是cd repo2而不是 repo1。 And step 6 should be git pull not push.第 6 步应该是git pull而不是 push。 Reworked list:重做清单:

  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

What I did:我做了什么:

  1. Clone repository to a folder将存储库克隆到文件夹
  2. cd existing-project
  3. open here a git terminal在这里打开一个 git 终端
  4. git remote set-url origin <NEW_GIT_REPO>
  5. git push -u origin --all
  6. git push origin --tags

If you are migrating from a github repo to another github repo如果您要从github 存储库迁移到另一个 github 存储库

I would recommend migrating with: git clone --bare <URL> as opposed to: git clone --mirror because if you mirror a github repo, it will not only clone the source code related things but also clone remaining pull requests and github references, causing an ! [remote rejected]我建议迁移: git clone --bare <URL>而不是: git clone --mirror因为如果你镜像一个github repo,它不仅会克隆源代码相关的东西,还会克隆剩余的pull request和github引用,导致! [remote rejected] ! [remote rejected] error when pushing. ! [remote rejected]推送时出错。

I am talking about this problem 我正在谈论这个问题

The procedure I recommend:我推荐的程序:

  1. git clone --bare <Old Repo Url>
  2. cd <path to cloned repo>
  3. git push --mirror <New Repo Url>

Successfully migrated all branches, history and source code to github new repo without any errors!成功将所有分支、历史和源代码迁移到 github new repo,没有任何错误!

For those using GitHub, you can import another repository (including history) via the web UI:对于使用 GitHub 的用户,您可以通过 Web UI 导入另一个存储库(包括历史记录):

https://github.com/new/import https://github.com/new/import

在此处输入图片说明

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

I am not very sure if what i did was right, but it worked anyways.我不太确定我所做的是否正确,但无论如何它都有效。 I renamed repo2 folder present inside repo1, and committed the changes.我重命名了 repo1 中的 repo2 文件夹,并提交了更改。 In my case it was just one repo which i wanted to merge , so this approach worked.就我而言,它只是我想合并的一个 repo,所以这种方法有效。 Later on, some path changes were done.后来,做了一些路径更改。

暂无
暂无

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

相关问题 将 Git 存储库的一部分移动到另一个存储库以保留历史记录 - Moving part of Git repository to another repository preserving history 在移动GIT存储库时保留GIT提交的历史记录 - Preserving history of GIT commits while moving a GIT repository 将GIT存储库(和提交历史记录)移动到当前为空的另一个存储库 - Moving GIT repository (and commit history) to another repository currently empty 将git存储库历史记录的一部分移动到另一个存储库中 - Moving part of a git repository’s history into another repository 如何将分支内容移动到另一个存储库中以保留历史记录,并避免复制原始存储库的完整历史记录? - How to move branch content to another repository preserving history and avoiding copying the full history of original repository? 将目录移动到另一个git存储库,同时保留相关历史记录 - Moving a directory to another git repository while retaining relevant history 将带历史记录的git存储库导出到另一个存储 - Export git repository with history into another repository 从TFSVC迁移到GIT - 具有部分历史记录的存储库? - Moving from TFSVC to GIT - Repository with partial History? 是否可以将一堆文件从一个git存储库移动到另一个git存储库,同时保留(大多数)历史记录? - Is it possible to move a bunch of files from one git repository to another while preserving (most) history? 如何将目录从一个git存储库复制到另一个目录,并保留历史记录? - How can I copy a directory from one git repository to another, preserving history?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM