如何備份git工作目錄到另外的遠端

紀錄一下怎樣備份工作目錄的

我的需求是把工作目錄下的branches備份到新開的遠端git server


首先會想到的是使用 git push 的 --all, --mirror參數

先說 --all , 這個參數會將git將局端的工作目錄的branch (refs/heads) 推到遠端

       --all
           Instead of naming each ref to push, specifies that all refs under
           refs/heads/ be pushed.

--mirror作的更多一點,根據man git push, 它將局端分支(local branch), 遠端分支, tags推到遠方repo; 然而在不是用 git clone --mirror下載的目錄這樣子只有把tags跟local branch推到了遠端.

       --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.

而且有時候我們並不想把local branch推到遠端,在我的案例來說我真正想要的是把aosp官方下載的repo備份到另一處.最後我這樣子下

 git push --prune git@example.com:/new-location.git +refs/remotes/origin/*:refs/heads/* +refs/tags/*:refs/tags/*

+號代表即使目標已存在也要覆蓋, --prune 則是把遠端與局端不批配的刪除掉,兩者搭配下會讓遠端repo (example.com:/new-location.git)與aosp的官方repo一致,且不會把還沒有commit的工作分支給上傳了. 適合使用在新建立的empty repo.


       --prune
           Remove remote branches that don’t have a local counterpart. For
           example a remote branch tmp will be removed if a local branch with
           the same name doesn’t exist any more. This also respects refspecs,
           e.g.  git push --prune remote refs/heads/*:refs/tmp/* would make
           sure that remote refs/tmp/foo will be removed if refs/heads/foo
           doesn’t exist.
       <refspec>...
           Specify what destination ref to update with what source object. The
           format of a <refspec> parameter is an optional plus +, followed by
           the source object <src>, followed by a colon :, followed by the
           destination ref <dst>.



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值