為了便于管理,Git 要求每個(gè)遠(yuǎn)程倉(cāng)庫(kù)必須指定一個(gè)別名。remote 命令就用于管理倉(cāng)庫(kù)別名的命令。
遠(yuǎn)程倉(cāng)庫(kù)指的是托管在網(wǎng)絡(luò)上的項(xiàng)目倉(cāng)庫(kù)。
通過(guò) clone 命令克隆下來(lái)的倉(cāng)庫(kù),默認(rèn)的別名是 origin。
主要涉及的操作有:增(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。
git remote 會(huì)列出每一個(gè)遠(yuǎn)程庫(kù)的別名??梢允褂?-v (verbose,詳細(xì))選項(xiàng)指定列出詳細(xì)信息。如下:
git remote show <name> 可查看某個(gè)遠(yuǎn)程庫(kù)的詳細(xì)信息
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/ 中。
-
可以對(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。 -
分支信息并不一定要存儲(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.