菜頭鎮樓
一般來說,有時候我們需要將代碼倉庫由一個地址放到另外個地址托管。而已有的代碼倉庫可能會比較龐雜,有大量的本地分支、遠程分支、TAG、Log、Stash等等,要想保留這些內容有2中方式。以下做簡單學習記錄。
- 修改遠程倉庫地址
- 直接遷移裸版本庫
前提:在新代碼倉庫建立空工程
簡易版
假設remote是origin,用git remote set_url 更換遠程指向地址。
git remote set-url origin remote_git_address
把remote_git_address改為新的空倉庫的git地址即可。
但是這樣處理的話,新的Push提交時,要想保留所有信息的話需要挨個提交對應分支、TAG到新倉庫,會比較麻煩。好處是不用重新改動工作目錄,也可以保留stash記錄、本地分支。如果簡單的項目可以這樣處理,也便于理解。但是復雜的項目就會顯得繁瑣。
改進版
直接從原倉庫拷貝一份裸版本庫到新倉庫,并結合簡單版的步驟,修改遠程地址指向即可保留所有原工作區間的東西。
-
添加--bare參數,克隆一份裸版本庫??梢钥吹皆诋斍澳夸浵螺d了xxx_demo.git這個文件夾,里面內容只是沒有工作目錄,而是版本控制目錄,這個目錄就保存了你所有的倉庫信息。
git clone --bare git@gitlab.com:vito_kong/xxx_demo.git
具體克隆的的三種方式的說明詳見傳送門
-
git clone origin-url
(non-bare) : you will get all of the tags copied, a local branchmaster (HEAD)
tracking a remote branchorigin/master
, and remote branchesorigin/next
,origin/pu
, andorigin/maint
. The tracking branches are set up so that if you do something likegit fetch origin
, they'll be fetched as you expect. Any remote branches (in the cloned remote) and other refs are completely ignored. -
git clone --bare origin-url
: you will get all of the tags copied, local branchesmaster (HEAD)
,next
,pu
, andmaint
, no remote tracking branches. That is, all branches are copied as is, and it's set up completely independent, with no expectation of fetching again. Any remote branches (in the cloned remote) and other refs are completely ignored. -
git clone --mirror origin-url
: every last one of those refs will be copied as-is. You'll get all the tags, local branchesmaster (HEAD)
,next
,pu
, andmaint
, remote branchesdevA/master
anddevB/master
, other refsrefs/foo/bar
andrefs/foo/baz
. Everything is exactly as it was in the cloned remote. Remote tracking is set up so that if you rungit remote update
all refs will be overwritten from origin, as if you'd just deleted the mirror and recloned it. As the docs originally said, it's a mirror. It's supposed to be a functionally identical copy, interchangeable with the original.
-
-
進入到剛才下載的xxx_demo.git目錄,并將其內內容以鏡像的方式推送到新倉庫,比如我這里的新倉庫是Coding.net
cd xxx_demo.git git push --mirror https://git.coding.net/antAtrom/xxx_demo_new.git
然后這時候就可以在網頁上看到新的倉庫內已經包含了所有的工作空間和代碼。
這時候可以選擇刪除原有的本地倉庫,新建目錄并從新倉庫clone代碼下來。但是這樣的話原有本地的暫存stash的修改記錄就會沒有了。所以可以結合簡化版的方式,在原來的本地目錄修改遠程地址就可以無差完成代碼托管倉庫遷移
git remote set-url origin remote_git_address