紀錄一下怎樣備份工作目錄的
我的需求是把工作目錄下的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>.