我有个服务器要关闭。我剩下要迁移的唯一东西是我的存储库。这个服务器被列为我的一个项目的原始(主)服务器。移动存储库以保存历史的正确方法是什么?
要添加新的回购位置,
git remote add new_repo_name new_repo_url
然后将内容推到新位置
git push new_repo_name master
最后去掉旧的
git remote rm origin
之后,你可以做什么bdonlan说,并编辑。Git /config文件修改new_repo_name为origin。如果您不删除原始(原始远程存储库),您可以简单地将更改推到新的repo with
git push new_repo_name master
我只是在一个简单的指导列表中转发别人说过的话。
Move the repository: Simply login to the new server, cd to the parent directory where you now want to hold the repository, and use rsync to copy from the old server: new.server> rsync -a -v -e ssh user@old.server.com:path/to/repository.git . 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 user@new.server.com:path/to/repository.git
可以使用如下命令:
git remote set-url --push origin new_repo_url
例子来自http://gitref.org/remotes/
$ git remote -v
github git@github.com:schacon/hw.git (fetch)
github git@github.com:schacon/hw.git (push)
origin git://github.com/github/git-reference.git (fetch)
origin git://github.com/github/git-reference.git (push)
$ git remote set-url --push origin git://github.com/pjhyett/hw.git
$ git remote -v
github git@github.com:schacon/hw.git (fetch)
github git@github.com:schacon/hw.git (push)
origin git://github.com/github/git-reference.git (fetch)
origin git://github.com/pjhyett/hw.git (push)
如果你想迁移所有的分支和标签,你应该使用以下命令:
git clone --mirror [oldUrl]
用所有分支克隆旧的回购
cd the_repo
git remote add remoteName newRepoUrl
设置一个新的遥控器
git push -f --tags remoteName refs/heads/*:refs/heads/*
将所有的refs放在refs/heads下(这可能是你想要的)
如果你想把一个#git仓库从一个服务器迁移到一个新的服务器,你可以这样做:
git clone OLD_REPOSITORY_PATH
cd OLD_REPOSITORY_DIR
git remote add NEW_REPOSITORY_ALIAS NEW_REPOSITORY_PATH
#check out all remote branches
for remote in `git branch -r | grep -v master `; do git checkout --track $remote ; done
git push --mirror NEW_REPOSITORY_PATH
git push NEW_REPOSITORY_ALIAS --tags
旧存储库中的所有远程分支和标记将被复制到新的存储库中。
单独运行该命令:
git push NEW_REPOSITORY_ALIAS
只复制一个主分支(只跟踪分支)到新的存储库。
我按照BitBucket上的说明移动了一个回购,所有分支都在那里。下面是#字符后面的步骤和解释:
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://git@bitbucket.org/<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
对我来说很管用。
看看GitHub上的这个食谱: https://help.github.com/articles/importing-an-external-git-repository
在发现git push -mirror之前,我尝试了许多方法。
效果好极了!
请按以下步骤操作:
git remote add new-origin <GIT URL>
git push --all new-origin
git push --tags new-origin
git remote rm origin
git remote rename new-origin origin
应该像这样简单:
git remote set-url origin git://new.url.here
通过这种方式,您可以保留新回购的名称来源-然后将旧的推到新回购,如其他答案中详细说明的那样。假设你独自工作,你有一个本地回购,你想镜像所有的cruft,你也可以(从你的本地回购内部)
git push origin --mirror # origin points to your new repo
但请参阅“git push——mirror”是否足以备份我的存储库?(在所有不使用——镜子但一次)。
更新为使用git push——mirror origin而不是评论中建议的git push -f origin。
这个方法对我来说完美无缺。
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 --mirror origin
我不得不提的是,这将创建当前回购的镜像,然后将其推到新位置。因此,对于大型回购或慢速连接,这可能需要一些时间。
这在其他答案中也有部分体现。
git clone --mirror git@oldserver:oldproject.git
cd oldproject.git
git remote add new git@newserver:newproject.git
git push --mirror new
如果你想保持所有的提交和分支从旧到新的回购
git clone --bare <old-repo-url>
cd <old-repo-directory>
git push --mirror <new-repo-url>
如果你想从一个源移动到另一个源,并在本地机器上保留当前源的备份,可以使用以下步骤:
首先在本地找到你想要移动的(git)文件夹 在线创建新的存储库 此步骤创建一个存储库,我们可以将代码推入其中
现在在文件夹里做
git remote get-url origin
上面的命令给出了当前的远程原点url,这对于在最后一步中将原点设置回很有用
git remote set-url origin git@github.newlocation:folder/newrepo.git
上面的命令将远程原点设置为新位置
git push --set-upstream origin develop
上面的命令将当前活动的本地分支推到使用branchname develop的远程分支。当然,它会保存所有历史,因为git也会推送所有历史。
git remote set-url origin <original old origin>
上面的命令将远程原点设置回您的当前原点:您需要这样做是因为您在现有文件夹中,并且您可能不希望将当前本地文件夹名称与您将要创建的用于克隆您刚刚推入的repo的新文件夹混淆。
希望这能有所帮助,
这是这个答案的一个变体,目前gitlab建议将一个git存储库从一个服务器“迁移”到另一个服务器。
让我们假设您的旧项目名为existing_repo,存储在existing_repo文件夹中。 在新服务器上创建一个回购。我们假设这个新项目的url是git@newserver:newproject.git 打开命令行界面,输入如下信息: cd existing_repo Git远程重命名原点old-origin Git远程添加源git@newserver:newproject.git Git push -u origin——all Git push -u origin——tags
这种方法的好处是不需要删除与旧服务器相对应的分支。
备注:
git copy http://a.com/old.git http://a.com/new.git
仅适用于例如,一个github到github拷贝,即保留在相同的git系统。当从例如github复制到gerrit时,这是不工作的。
除此之外,它还很好用,因为它自动复制分支、标记和子模块。
推荐文章
- 为什么我需要显式地推一个新分支?
- 如何撤消最后的git添加?
- Rubymine:如何让Git忽略Rubymine创建的.idea文件
- Gitignore二进制文件,没有扩展名
- Git隐藏错误:Git隐藏弹出并最终与合并冲突
- 了解Git和GitHub的基础知识
- 没有。Git目录的Git克隆
- Git与Mercurial仓库的互操作性
- 忽略git中修改(但未提交)的文件?
- “git restore”命令是什么?“git restore”和“git reset”之间有什么区别?
- Git合并与强制覆盖
- Git拉另一个分支
- 在Bash命令提示符上添加git分支
- 如何更改Git日志日期格式
- git pull -rebase和git pull -ff-only之间的区别