在實(shí)際的開發(fā)中,有可能遇到需要將A倉庫的某個(gè)branch的代碼獨(dú)立成為一個(gè)新的倉庫B。我們需要的不僅僅是這個(gè)branch的最新提交的代碼,而是需要它的整個(gè)提交歷史。這種需求對(duì)于git來說非常簡(jiǎn)單,主要依靠git remote命令來實(shí)現(xiàn)。
把當(dāng)前代碼提交到另一個(gè)遠(yuǎn)程git倉庫
假如倉庫repo_a當(dāng)前位于branch_a,要求將branch_a的整個(gè)數(shù)據(jù)(包括提交歷史)全部提取出來,并建立一個(gè)新的倉庫repo_b。這里假設(shè)倉庫repo_b已經(jīng)被建立。
git remote add基本語法如下。name和url是必須的。
<pre>
git remote add [-t <branch>] [-m <master>] [-f] [--[no-]tags] [--mirror=<fetch|push>] <name> <url>
</pre>
1、 將倉庫repo_b的URL添加到工作倉庫的remote。
<pre>
git remote add origin_repo_b git@server_ip:/path/repo_b.git
</pre>
(origin_repo_b:自己起的名字,只要不與現(xiàn)有的remote名重復(fù)即可)
(git@server_ip:/path/repo_b.git:repo_b的遠(yuǎn)程路徑)
2、將代碼推送到遠(yuǎn)程repo_b
<pre>
git push origin_repo_b branch_a
</pre>
(origin_repo_b:遠(yuǎn)程倉庫repo_b的名字)
(branch_a:倉庫repo_a的的branch_a分支)
3、克隆倉庫repo_b,檢查是否push成功
<pre>
git clone git@server_ip:/path/repo_b.git
</pre>
將一份相同的代碼提交到多個(gè)不同的git托管服務(wù)器(多個(gè)git倉庫)
方法非常相似,多使用到了命令<pre>git remote set-url --add [--push] <name> <newurl></pre>
假設(shè)在遠(yuǎn)程的git服務(wù)器上又新增了一個(gè)倉庫repo_c,現(xiàn)在要求repo_b、repo_c提交的代碼必須一致。
1、將遠(yuǎn)程repo_c配置到當(dāng)前的工作的本地git倉庫中
<pre>
git remote set-url --add origin_repo_b git@192.168.1.101:~/project/repo_c.git
</pre>
這句話的意思是,將遠(yuǎn)程倉庫git@192.168.1.101:~/project/repo_c.git也加入到origin_repo_b這個(gè)名字下面管理
2、將代碼同時(shí)提交到遠(yuǎn)程倉庫repo_b和遠(yuǎn)程倉庫repo_c
<pre>
git push origin_repo_b branch_a
</pre>