您当前的位置: 首页 > 学无止境 > 心得笔记 网站首页心得笔记
29 如何将Git仓库备份到本地...
发布时间:2020-07-25 09:45:45编辑:雪饮阅读()
备份
用git clone可以备份git仓库,该命令支持哑协议和智能协议
哑协议备份
我们有一个空目录web2用来模拟一个远端做为备份的存储空间,我们使用哑协议备份,而我们要进行备份的git所在目录是web,则有
xy@DESKTOP-BG9HNHK MINGW64 ~/Desktop/web/web2
$ git clone --bare /c/Users/xy/Desktop/web/web/.git ya.git
Cloning into bare repository 'ya.git'...
done.
智能协议备份
同理,这里有一个空目录web3用来模拟另外一个远端也做为备份的存储空间,这里我们使用智能协议备份,而我们要进行备份的git所在目录依然是web,则有
xy@DESKTOP-BG9HNHK MINGW64 ~/Desktop/web/web3
$ git clone --bare file:///c/Users/xy/Desktop/web/web/.git zhineng.git
Cloning into bare repository 'zhineng.git'...
remote: Enumerating objects: 62, done.
remote: Counting objects: 100% (62/62), done.
remote: Compressing objects: 100% (51/51), done.
remote: Total 62 (delta 9), reused 4 (delta 1), pack-reused 0
Receiving objects: 100% (62/62), 314.30 KiB | 6.29 MiB/s, done.
Resolving deltas: 100% (9/9), done.
总结:
上面两种备份方式我们都有使用—bare。
详细说一下使用 --bare 参数的含义,使用 --bare 参数初始化的仓库,我们一般称之为裸仓库, 因为这样创建的仓库并不包含 工作区 , 也就是说,我们并不能在这个目录下执行我们一般使用的 Git 命令。
上面两种方式对比,发现智能协议是有备份进度的,而且据说按速度对比时第二种方式的速度快。
远端
查看远端列表
用git remote -v可以查看本地已经添加过的远端列表,如
xy@DESKTOP-BG9HNHK MINGW64 ~/Desktop/web/web (branch3|REBASE 4/5)
$ git remote -v
origin https://github.com/xueyin220807/web.git (fetch)
origin https://github.com/xueyin220807/web.git (push)
添加远端
这里需要对上面智能备份做下补充,上面我们把智能备份备份在web3上面,其实也可以直接和哑协议备份到一块,毕竟一个是ya.git一个是zhineng.git
接下来我们看看如何添加远端,添加远端只需要用remote add然后设置一个远端名和要添加的远端的地址,如
xy@DESKTOP-BG9HNHK MINGW64 ~/Desktop/web/web (branch3|REBASE 4/5)
$ git remote add zhineng file:///c/Users/xy/Desktop/web/web3/zhineng.git
xy@DESKTOP-BG9HNHK MINGW64 ~/Desktop/web/web (branch3|REBASE 4/5)
$ git remote -v
origin https://github.com/xueyin220807/web.git (fetch)
origin https://github.com/xueyin220807/web.git (push)
zhineng file:///c/Users/xy/Desktop/web/web3/zhineng.git (fetch)
zhineng file:///c/Users/xy/Desktop/web/web3/zhineng.git (push)
远端仓库与本地仓库关于分支的对比
本地
xy@DESKTOP-BG9HNHK MINGW64 ~/Desktop/web/web (branch3|REBASE 4/5)
$ git branch -av
branch2 9979ccc branch2dir3
branch3 b07d738 modify 2.txt
* master 48142d3 [ahead 1] modified index.html
temp 48142d3 modified index.html
remotes/origin/HEAD -> origin/master
remotes/origin/master b0babbd index
远端
xy@DESKTOP-BG9HNHK MINGW64 ~/Desktop/web/web3/zhineng.git (BARE:master)
$ git branch -av
branch2 9979ccc branch2dir3
branch3 b07d738 modify 2.txt
* master 48142d3 modified index.html
temp 48142d3 modified index.html
发现分支数量是不同的,可能是刚才备份到远端时候的—bare参数所影响吧
推送到远端
首先在本地建立一个分支
xy@DESKTOP-BG9HNHK MINGW64 ~/Desktop/web/web (branch3|REBASE 4/5)
$ git branch newBranch1
然后切换本地分支到newBranch1中并随便建立几个文件并提交
xy@DESKTOP-BG9HNHK MINGW64 ~/Desktop/web/web (branch3|REBASE 4/5)
$ git checkout newBranch1
Switched to branch 'newBranch1'
xy@DESKTOP-BG9HNHK MINGW64 ~/Desktop/web/web (branch3|REBASE 4/5)
$ touch 220807.txt
xy@DESKTOP-BG9HNHK MINGW64 ~/Desktop/web/web (branch3|REBASE 4/5)
$ git add 220807.txt
xy@DESKTOP-BG9HNHK MINGW64 ~/Desktop/web/web (branch3|REBASE 4/5)
$ git commit -m "220807"
[newBranch1 474a767] 220807
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 220807.txt
然后我们假定要将newBranch1这个分支推送到智能协议创建的远端,则有
xy@DESKTOP-BG9HNHK MINGW64 ~/Desktop/web/web (branch3|REBASE 4/5)
$ git push zhineng
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 12 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 262 bytes | 262.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0), pack-reused 0
To file:///c/Users/xy/Desktop/web/web3/zhineng.git
* [new branch] newBranch1 -> newBranch1
此时我们再来查看远端,会发现刚才本地仓库新建的一个分支已经被成功推送到了远端仓库来了
xy@DESKTOP-BG9HNHK MINGW64 ~/Desktop/web/web3/zhineng.git (BARE:master)
$ git branch -av
branch2 9979ccc branch2dir3
branch3 b07d738 modify 2.txt
* master 48142d3 modified index.html
newBranch1 474a767 220807
temp 48142d3 modified index.html
关键字词:git,备份,克隆,远端,remote,push
下一篇:31 配置公私钥