一般來(lái)說(shuō)惰说,有時(shí)候我們需要將代碼倉(cāng)庫(kù)由一個(gè)地址放到另外個(gè)地址托管。而已有的代碼倉(cāng)庫(kù)可能會(huì)比較龐雜缘回,有大量的本地分支吆视、遠(yuǎn)程分支、TAG努酸、Log曙寡、Stash等等妙痹,要想保留這些內(nèi)容有2中方式。以下做簡(jiǎn)單學(xué)習(xí)記錄授滓。
- 修改遠(yuǎn)程倉(cāng)庫(kù)地址
- 直接遷移裸版本庫(kù)
前提:在新代碼倉(cāng)庫(kù)建立空工程
簡(jiǎn)易版
假設(shè)remote是origin,用git remote set_url 更換遠(yuǎn)程指向地址肆糕。
git remote set-url origin remote_git_address
把remote_git_address改為新的空倉(cāng)庫(kù)的git地址即可般堆。
但是這樣處理的話,新的Push提交時(shí)诚啃,要想保留所有信息的話需要挨個(gè)提交對(duì)應(yīng)分支淮摔、TAG到新倉(cāng)庫(kù),會(huì)比較麻煩始赎。好處是不用重新改動(dòng)工作目錄和橙,也可以保留stash記錄、本地分支极阅。如果簡(jiǎn)單的項(xiàng)目可以這樣處理胃碾,也便于理解。但是復(fù)雜的項(xiàng)目就會(huì)顯得繁瑣筋搏。
改進(jìn)版
直接從原倉(cāng)庫(kù)拷貝一份裸版本庫(kù)到新倉(cāng)庫(kù)仆百,并結(jié)合簡(jiǎn)單版的步驟,修改遠(yuǎn)程地址指向即可保留所有原工作區(qū)間的東西奔脐。
-
添加--bare參數(shù)俄周,克隆一份裸版本庫(kù)吁讨。可以看到在當(dāng)前目錄下載了xxx_demo.git這個(gè)文件夾峦朗,里面內(nèi)容只是沒(méi)有工作目錄建丧,而是版本控制目錄,這個(gè)目錄就保存了你所有的倉(cāng)庫(kù)信息波势。
git clone --bare git@gitlab.com:vito_kong/xxx_demo.git
具體克隆的的三種方式的說(shuō)明詳見(jiàn)傳送門
-
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.
-
-
進(jìn)入到剛才下載的xxx_demo.git目錄翎朱,并將其內(nèi)內(nèi)容以鏡像的方式推送到新倉(cāng)庫(kù),比如我這里的新倉(cāng)庫(kù)是Coding.net
cd xxx_demo.git git push --mirror https://git.coding.net/antAtrom/xxx_demo_new.git
然后這時(shí)候就可以在網(wǎng)頁(yè)上看到新的倉(cāng)庫(kù)內(nèi)已經(jīng)包含了所有的工作空間和代碼尺铣。
這時(shí)候可以選擇刪除原有的本地倉(cāng)庫(kù)拴曲,新建目錄并從新倉(cāng)庫(kù)clone代碼下來(lái)。但是這樣的話原有本地的暫存stash的修改記錄就會(huì)沒(méi)有了凛忿。所以可以結(jié)合簡(jiǎn)化版的方式澈灼,在原來(lái)的本地目錄修改遠(yuǎn)程地址就可以無(wú)差完成代碼托管倉(cāng)庫(kù)遷移
git remote set-url origin remote_git_address