Git仓库迁移而不丢失log的方法
要求能保留原先的commit记录,应该如何迁移呢?
同时,本地已经clone了原仓库,要配置成新的仓库地址,该如何修改呢?
注意:如果使用了代码审核工具Gerrit,那么在进行操作之前需要将Gerrit关掉,等成功恢复后再将Gerrit开户即可
1、使用git push --mirror
先了解一些git的基本参数介绍
git clone --bare

GIT-CLONE(1)                      Git Manual                      GIT-CLONE(1)
 
NAME
       git-clone - Clone a repository into a new directory
 
SYNOPSIS
       git clone [--template=<template_directory>]
                 [-l] [-s] [--no-hardlinks] [-q] [-n] [--bare] [--mirror]
                 [-o <name>] [-b <name>] [-u <upload-pack>] [--reference
<repository>]
                 [--depth <depth>] [--recursive] [--] <repository>
[<directory>]
        --bare
            Make a bare GIT repository. That is, instead of creating
<directory> and placing the administrative files
           in <directory>/.git, make the <directory> itself the $GIT_DIR.
This obviously implies the -n because there
           is nowhere to check out the working tree. Also the branch heads at
the remote are copied directly to
           corresponding local branch heads, without mapping them to
refs/remotes/origin/. When this option is used,
           neither remote-tracking branches nor the related configuration
variables are created.

git push --mirror

--mirror
           Instead of naming each ref to push, specifies that all refs under
refs/ (which includes but is not limited
           to refs/heads/, refs/remotes/, and refs/tags/) be mirrored to the
remote repository. Newly created local
           refs will be pushed to the remote end, locally updated refs will
be force updated on the remote end, and
           deleted refs will be removed from the remote end. This is the
default if the configuration option
           remote.<remote>.mirror is set.

几种不同git clone 区别:

裸克隆和普通(非裸)克隆之间的差异 - 非裸克隆设置远程跟踪分支,仅创建本地分支HEAD,而裸克隆直接复制分支。

假设起源有几个分支(master (HEAD),next,pu和maint),一些标签(v1,v2,v3),一些远程分支机构(devA/master,
devB/master),以及其他一些裁判(refs/foo/bar,refs/foo/baz,这可能是笔记,储物箱,其他开发者的命名空间,谁知道)。

*
git clone origin-url(非裸):您将得到所有复制的标签,一个本地分支master (HEAD)追踪远程分支origin/master
和远程分支origin/next,origin/pu和origin/maint。设置了跟踪分支,这样如果你做了类似的事情git fetch origin
,它们就会像你期望的那样被提取。任何远程分支(在克隆的远程中)和其他引用都被完全忽略。

*
git clone --bare origin-url:您将获得全部复制的标签,地方分支机构master (HEAD),next,pu,和maint
,没有远程跟踪分支。也就是说,所有分支都按原样复制,并且它设置为完全独立,不期望再次获取。任何远程分支(在克隆的远程中)和其他引用都被完全忽略。

*
git clone --mirror origin-url:这些引用中的每一个都将按原样复制。你会得到所有的标签,地方分支机构master (HEAD),
next,pu,和maint,远程分支机构devA/master和devB/master其他裁判refs/foo/bar和refs/foo/baz
。一切都与克隆的遥控器完全一样。设置远程跟踪,以便在运行时,git remote update
所有引用都将从原点覆盖,就像您刚删除镜像并重新克隆它一样。正如文档最初所说,它是一面镜子。它应该是功能相同的副本,可与原始版本互换。

1、建立新仓库
1). 从原地址克隆一份裸版本库,比如原本托管于 GitHub,或者是本地的私有仓库
git clone --bare git://192.168.10.XX/git_repo/project_name.git
2). 然后到新的 Git 服务器上创建一个新项目,比如 GitCafe,亦或是本地的私有仓库,如192.168.20.XX
su - git
cd /path/to/path/
mkdir new_project_name.git
git init --bare new_project_name.git
3). 以镜像推送的方式上传代码到 GitCafe 服务器上。
请确保已经添加了公钥到新的机器上
cd project_name.git
git push --mirror git@192.168.20.XX/path/to/path/new_project_name.git
4). 删除本地代码
cd ..
rm -rf project_name.git
5). 到新服务器上找到 Clone 地址,直接Clone到本地就可以了。
git clone git@192.168.20.XX/path/to/path/new_project_name.git
这种方式可以保留原版本库中的所有内容。

2、切换remote_url
先查看remote的名字

git branch -r
假设你的remote是origin,用git remote set_url 更换地址

git remote set-url origin remote_git_address
remote_git_address更换成你的新的仓库地址。

第二种切换remote_url的方法更直接,直接更改.git/conf配置文件里的ip地址就行。

技术
下载桌面版
GitHub
百度网盘(提取码:draw)
Gitee
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:ixiaoyang8@qq.com
QQ群:766591547
关注微信