remote 命令

為了便于管理,Git 要求每個(gè)遠(yuǎn)程倉(cāng)庫(kù)必須指定一個(gè)別名。remote 命令就用于管理倉(cāng)庫(kù)別名的命令。

  1. 遠(yuǎn)程倉(cāng)庫(kù)指的是托管在網(wǎng)絡(luò)上的項(xiàng)目倉(cāng)庫(kù)。

  2. 通過(guò) clone 命令克隆下來(lái)的倉(cāng)庫(kù),默認(rèn)的別名是 origin。

  3. 主要涉及的操作有:增(add)、刪(remove)、改(rename,set-url)、查(show , get-url)。

常用命令

命令 解釋 選項(xiàng)
git remote add <name> <url> 添加新的遠(yuǎn)程庫(kù)
git remote remove <name> 刪除指定的倉(cāng)庫(kù)
git remote rename <old> <new> 將倉(cāng)庫(kù)別名重命名為 new
git remote set-url <name> <newurl> 將倉(cāng)庫(kù)的 url 修改為 newurl
git remote 列出所有的遠(yuǎn)程庫(kù) -v 表示查看每個(gè)庫(kù)的 url
git remote show <name> 查看指定庫(kù)的詳細(xì)信息
git remote get-url <name> 獲得指定倉(cāng)庫(kù)的 url

在操作時(shí),將別名與 url 的關(guān)系理解為 Map 集合 —— 其中別名是 key,url 是 value。


添加

git remote add <name> <url>:添加一個(gè)遠(yuǎn)程庫(kù),同時(shí)為該遠(yuǎn)程庫(kù)指定別名,使用別名指代遠(yuǎn)程庫(kù)地址。

git remote add images https://github.com/birdandcliff/images.git

以后可以使用 images 代替對(duì)應(yīng)的url。

使用該命令可以將本地已有的文件與遠(yuǎn)程倉(cāng)庫(kù)關(guān)聯(lián),從而可以將本地已有的文件提交到遠(yuǎn)程倉(cāng)庫(kù)中。


刪除

使用 git remote remove <name> 移除別名對(duì)應(yīng)的倉(cāng)庫(kù)。

$ git remote
demo
newo

$ git remote remove newo
$ git remote
demo

修改

修改別名

使用 git remote rename <old> <name> 修改某個(gè)遠(yuǎn)程庫(kù)的別名。

對(duì)遠(yuǎn)程庫(kù)重命名后,對(duì)應(yīng)的分支也會(huì)發(fā)生變化,將其中關(guān)于別名部分換成新別名。

$ git remote -v
demo    https://github.com/birdandcliff/seturl.git (fetch)
demo    https://github.com/birdandcliff/seturl.git (push)

$ git remote -v
demo    https://github.com/birdandcliff/seturl.git (fetch)
demo    https://github.com/birdandcliff/seturl.git (push)

$ git remote rename demo demo2
$ git remote -v
demo2   https://github.com/birdandcliff/seturl.git (fetch)
demo2   https://github.com/birdandcliff/seturl.git (push)

可以看出, url 沒(méi)有發(fā)生變化,但對(duì)應(yīng)的別名已經(jīng)修改過(guò)了。

修改 url

使用 git remote set-url <name> <newurl> 將指定的遠(yuǎn)程倉(cāng)庫(kù)地址修改為 newurl。

$ git remote -v
demo    https://github.com/birdandcliff/gitdemo.git (fetch)
demo    https://github.com/birdandcliff/gitdemo.git (push)

$ git remote set-url demo https://github.com/birdandcliff/seturl.git
$ git remote -v
demo    https://github.com/birdandcliff/seturl.git (fetch)
demo    https://github.com/birdandcliff/seturl.git (push)

修改倉(cāng)庫(kù)地址后,可以將本地文件提交提交到新的 url 中。


查看

可以查看所有的別名,也可以查看所有的別名與 url,還能通過(guò)別名查看指定的 url。

  1. git remote 會(huì)列出每一個(gè)遠(yuǎn)程庫(kù)的別名??梢允褂?-v (verbose,詳細(xì))選項(xiàng)指定列出詳細(xì)信息。如下:

  2. git remote show <name> 可查看某個(gè)遠(yuǎn)程庫(kù)的詳細(xì)信息

  3. git remote get-url <name> 根據(jù) name 查看指定的 url

$ git remote
newo

$ git remote -v
newo    https://github.com/birdandcliff/seturl.git (fetch)
newo    https://github.com/birdandcliff/seturl.git (push)

$ git remote get-url newo
https://github.com/birdandcliff/seturl.git

$ git remote show newo
* remote newo
  Fetch URL: https://github.com/birdandcliff/seturl.git
  Push  URL: https://github.com/birdandcliff/seturl.git
  HEAD branch: master
  Remote branch:
    master tracked
  Local branch configured for 'git pull':
    master merges with remote master
  Local ref configured for 'git push':
    master pushes to master (up to date)

remote 的配置信息

參考

使用 git remote add <別名> url 后,會(huì)在 .git/config 文件中添加如下信息:

[remote "demo"]
    url = https://github.com/birdandcliff/gitdemo.git
    fetch = +refs/heads/*:refs/remotes/demo/*

fetch 的格式由一個(gè)可選的 + 號(hào)和緊隨其后的 <src>:<dst> 組成。其中 <src> 代表遠(yuǎn)程倉(cāng)庫(kù)中的引用;<dst> 是那些遠(yuǎn)程引用在本地所對(duì)應(yīng)的位置。 + 號(hào)告訴 Git 即使在不能快進(jìn)的情況下也要(強(qiáng)制)更新引用。

因此,Git 會(huì)獲取 refs/heads/ 下面的所有引用,并將它寫(xiě)入到本地的 refs/remotes/demo/ 中。

  1. 可以對(duì) fetch 進(jìn)行手動(dòng)修改。如將 fetch 行修改如下:

    fetch = +refs/heads/master:refs/remotes/demo/master
    

    那么使用 git fetch 時(shí),就只會(huì)拉取遠(yuǎn)程倉(cāng)庫(kù)的 master。

  2. 分支信息并不一定要存儲(chǔ)在 demo 目錄下。可以在該目錄下任意指定子目錄。如:

    [remote "demo"]
        url = https://github.com/birdandcliff/gitdemo.git
        fetch = +refs/heads/re:refs/remotes/demo/devlocal
        fetch = +refs/heads/tra:refs/remotes/demo/xx/tra
    

    遠(yuǎn)程的 tra 分支存儲(chǔ)的路徑就在 demo/xx 目錄下,而不是直接位于 demo 目錄下。

    引用 tra 遠(yuǎn)程分支時(shí),也需要寫(xiě)成 demo/xx/tra,不能直接寫(xiě)成 demo/tra 形式。如:

    $git branch -u demo/xx/tra
    Branch dev set up to track remote branch tra from demo.
    
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開(kāi)發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見(jiàn)模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,554評(píng)論 19 139
  • 1. GIT命令 git init在本地新建一個(gè)repo,進(jìn)入一個(gè)項(xiàng)目目錄,執(zhí)行g(shù)it init,會(huì)初始化一個(gè)re...
    江邊一蓑煙閱讀 891評(píng)論 0 0
  • 1.git的安裝 1.1 在Windows上安裝Git msysgit是Windows版的Git,從https:/...
    落魂灬閱讀 12,836評(píng)論 4 54
  • 龍: 7月30日那天,我看了慶八一閱兵式,沙場(chǎng)點(diǎn)兵,獵獵軍旗紅,我軍將士銳可不擋,豪氣沖云天!此情此景,不僅讓我心...
    九月流云閱讀 815評(píng)論 35 25
  • 早上匆匆忙忙地出了門(mén),奔向北門(mén)橋去外婆家。車(chē)到了張家界市內(nèi),順便麥當(dāng)勞買(mǎi)了些吃的。真不應(yīng)該買(mǎi)的,后悔死了!吃了不僅...
    椒花柏酒閱讀 179評(píng)論 0 0

友情鏈接更多精彩內(nèi)容