git remote 命令

Git 基本操作Git 基本操作

git remote 命令用于一些对远程仓库的操作。

 

1. git remote 命令

git remote 命令可以查看当前配置中有哪些远程仓库,列出每个远程库的简短名字。

在克隆完某个项目后,我们可以看到一个名为 origin 的远程库,git 默认使用这个名字来标识克隆的原始仓库。

我们克隆一个远程仓库项目,查看一下本地配置中的远程仓库列表:

$ git clone https://github.com/owenliang/go-websocket.git
$ cd go-websocket

# git remote 不带参数,列出已经存在的远程分支

$ git remote
origin

其中:origin 是远程仓库的默认别名,在很多命令中可以直接代替远程仓库的 Url 使用。

 

2. git remote -v 命令

列出每个远程库的详细信息,在每一个名字后面列出其远程 url。

我们查看一下本地配置中的远程仓库详细列表:

$ git remote -v

origin	https://github.com/owenliang/go-websocket.git (fetch)
origin	https://github.com/owenliang/go-websocket.git (push)

 

3. git remote add 命令

用于添加本地配置中的一个远程仓库。

git remote add [shortname] [url]

[shortname] 为远程仓库的别名,比如 origin 是系统默认的远程仓库别名。

[url] 为远程仓库的 Url 地址,比如 https://github.com/owenliang/go-websocket.git。

我们添加一个远程仓库,别名为 myproject:

$ git remote add myproject https://github.com/owenliang/go-websocket.git

$ git remote -v
myproject	https://github.com/owenliang/go-websocket.git (fetch)
myproject	https://github.com/owenliang/go-websocket.git (push)
origin	https://github.com/owenliang/go-websocket.git (fetch)
origin	https://github.com/owenliang/go-websocket.git (push)

 

4. git remote rm 命令

用于删除本地配置中的一个远程仓库。

git remote rm [shortname]

[shortname] 为远程仓库的别名,比如 myproject。

我们删除本地配置中的一个远程仓库 myproject:

$ git remote rm myproject

$ git remote -v
origin	https://github.com/owenliang/go-websocket.git (fetch)
origin	https://github.com/owenliang/go-websocket.git (push)

 

5. git remote rename 命令

用于重命名本地配置中的一个远程仓库。

git remote rename [shortname] [new shortname]

[shortname] 为远程仓库的别名。

[new shortname] 为远程仓库的新的别名。

我们将本地配置中的远程仓库 myproject 改名为 myproject1:

$ git remote rename myproject myproject1

$ git remote -v
myproject1	https://github.com/owenliang/go-websocket.git (fetch)
myproject1	https://github.com/owenliang/go-websocket.git (push)
origin	https://github.com/owenliang/go-websocket.git (fetch)
origin	https://github.com/owenliang/go-websocket.git (push)

 

Git 基本操作Git 基本操作