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