如何将 GIT 存储库从一台服务器迁移到新服务器

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

How to migrate GIT repository from one server to a new one

git

提问by cjibo

I have a server that I'm taking down. The only thing I have left to migrate is my repository. This server is listed as the origin (master) for one of my projects. What is the proper way to move the repository to keep the history.

我有一个服务器要关闭。我唯一要迁移的是我的存储库。该服务器被列为我的一个项目的源(主)。移动存储库以保留历史记录的正确方法是什么。

采纳答案by Koby

To add the new repo location,

要添加新的回购位置,

git remote add new_repo_name new_repo_url

Then push the content to the new location

然后将内容推送到新位置

git push new_repo_name master

Finally remove the old one

最后删除旧的

git remote rm origin

After that you can do what bdonlan said and edit the.git/config file to change the new_repo_name to origin. If you don't remove the origin (original remote repository), you can simply just push changes to the new repo with

之后,您可以按照 bdonlan 所说的操作并编辑 .git/config 文件以将 new_repo_name 更改为 origin。如果您不删除源(原始远程存储库),则只需将更改推送到新存储库

git push new_repo_name master

回答by jzwiener

If you want to migrate all branches and tags you should use the following commands:

如果要迁移所有分支和标签,应使用以下命令:

git clone --mirror [oldUrl]

to clone the old repo with all branches

克隆所有分支的旧仓库

cd the_repo
git remote add remoteName newRepoUrl

to setup a new remote

设置一个新的遥控器

git push -f --tags remoteName refs/heads/*:refs/heads/*

to push all refs under refs/heads (which is probably what you want)

将所有 refs 推送到 refs/heads 下(这可能是您想要的)

回答by Roberto

This worked for me flawlessly.

这对我来说完美无缺

git clone --mirror <URL to my OLD repo location>
cd <New directory where your OLD repo was cloned>
git remote set-url origin <URL to my NEW repo location>
git push -f origin

I have to mention though that this creates a mirror of your current repo and then pushes that to the new location. Therefore, this can take some time for large repos or slow connections.

我不得不提一下,这会创建您当前存储库的镜像,然后将其推送到新位置。因此,对于大型存储库或慢速连接,这可能需要一些时间

回答by bdonlan

Copy it over. It's really that simple. :)

复制过来。真的就是这么简单。:)

On the client side, just edit .git/config in the client's local repo to point your remotes to the new URL as necessary.

在客户端,只需在客户端的本地存储库中编辑 .git/config 以根据需要将您的遥控器指向新的 URL。

回答by Ian Walters

This is sort of done in parts in some of the other answers.

这是在其他一些答案中部分完成的。

git clone --mirror git@oldserver:oldproject.git
cd oldproject.git
git remote add new git@newserver:newproject.git
git push --mirror new

回答by Juan A. Navarro

I'm just reposting what others have said, in a simple to follow list of instructions.

我只是在简单易懂的说明列表中重新发布其他人所说的内容。

  1. Move the repository:Simply login to the new server, cdto the parent directory where you now want to hold the repository, and use rsyncto copy from the old server:

    new.server> rsync -a -v -e ssh [email protected]:path/to/repository.git .
    
  2. Make clients point to the new repository:Now on each client using the repository, just remove the pointer to the old origin, and add one to the new one.

    client> git remote rm origin
    client> git remote add origin [email protected]:path/to/repository.git
    
  1. 移动存储库:只需登录到新服务器,cd进入您现在要保存存储库的父目录,然后使用rsync从旧服务器复制:

    new.server> rsync -a -v -e ssh [email protected]:path/to/repository.git .
    
  2. 使客户端指向新的存储库:现在在每个使用存储库的客户端上,只需删除指向旧源的指针,并将其添加到新源。

    client> git remote rm origin
    client> git remote add origin [email protected]:path/to/repository.git
    

回答by John Smith

Take a look at this recipe on GitHub: https://help.github.com/articles/importing-an-external-git-repository

看看 GitHub 上的这个秘籍:https: //help.github.com/articles/importing-an-external-git-repository

I tried a number of methods before discovering git push --mirror.

在发现git push --mirror.

Worked like a charm!

像魅力一样工作!

回答by Laryx Decidua

I followed the instructions on BitBucket to move a repo with all its branches there. Here come the steps with explanations following the #character:

我按照 BitBucket 上的说明移动了一个 repo 及其所有分支。下面是#字符后面的步骤和解释:

cd path/to/local/repo
git remote remove origin # to get rid of the old setting, this was not in the BitBucket instructions
git remote add origin ssh://[email protected]/<username>/<newrepo> # modify URL as needed
git push -u origin --all # pushes _ALL_ branches in one go
git push -u origin --tags # pushes _ALL_ tags in one go

Worked nicely for me.

对我来说有效得很好。

回答by Boush

Please follow the steps:

请按照以下步骤操作:

  • git remote add new-origin
  • git push --all new-origin
  • git push --tags new-origin
  • git remote rm origin
  • git remote rename new-origin origin
  • git remote 添加新来源
  • git push --all new-origin
  • git push --tags 新来源
  • git远程rm原点
  • git remote 重命名新来源

回答by Mr_and_Mrs_D

Should be as simple as:

应该很简单:

git remote set-url origin git://new.url.here

This way you keep the name originfor your new repo - then push to the new repo the old one as detailed in the other answers. Supposing you work alone and you have a local repo you want to mirror with all your cruft in it, you might as well (from inside your local repo)

通过这种方式,您可以保留origin新存储库的名称- 然后将旧存储库推送到新存储库,如其他答案中所述。假设你一个人工作,并且你有一个本地仓库,你想用你所有的东西来镜像,你也可以(从你的本地仓库内部)

git push origin --mirror # origin points to your new repo

but see Is "git push --mirror" sufficient for backing up my repository?(in all don't use --mirrorbut once).

但请参阅“git push --mirror”是否足以备份我的存储库?(在所有不要使用,--mirror但一次)。