git remote 管理远程仓库
git remote 命令用于查看、添加、修改、删除远程仓库的连接。
基本用法
Bash
# 查看所有远程仓库
git remote
# 查看远程仓库详情
git remote -v
# 添加远程仓库
git remote add <name> <url>
# 删除远程仓库
git remote remove <name>
查看远程仓库
Bash
# 列出远程仓库名
$ git remote
origin
# 详细显示(含 URL)
$ git remote -v
origin https://github.com/user/repo.git (fetch)
origin https://github.com/user/repo.git (push)
添加远程仓库
Bash
# 添加名为 origin 的远程仓库
git remote add origin https://github.com/user/repo.git
# 添加多个远程仓库
git remote add upstream https://github.com/original/repo.git
git remote add backup https://backup.server/repo.git
# 查看结果
$ git remote -v
origin https://github.com/user/repo.git (fetch)
origin https://github.com/user/repo.git (push)
upstream https://github.com/original/repo.git (fetch)
upstream https://github.com/original/repo.git (push)
修改远程仓库
Bash
# 重命名远程仓库
git remote rename <old-name> <new-name>
git remote rename origin github
# 修改远程仓库 URL
git remote set-url <name> <new-url>
git remote set-url origin https://new.url/repo.git
# 修改推送 URL(与拉取不同)
git remote set-url --push <name> <push-url>
删除远程仓库
Bash
# 删除远程仓库连接
git remote remove <name>
git remote remove backup
# 或使用 rm(简写)
git remote rm backup
查看远程仓库信息
Bash
# 查看详细信息
git remote show origin
# 输出示例
* remote origin
Fetch URL: https://github.com/user/repo.git
Push URL: https://github.com/user/repo.git
HEAD branch: main
Remote branches:
main tracked
dev tracked
Local branches configured for 'git pull':
main merges with remote main
Local refs configured for 'git push':
main pushes to main
远程仓库名称约定
| 名称 | 用途 |
|---|---|
| origin | 克隆时的默认远程仓库 |
| upstream | 原始仓库(fork 场景) |
| backup | 备份仓库 |
克隆仓库时自动创建 origin 远程连接,无需手动添加。
要点总结
git remote -v查看远程仓库详情git remote add <name> <url>添加远程仓库git remote set-url修改 URLgit remote remove <name>删除远程仓库- origin 是默认远程仓库名称
📝 发现内容有误?点击此处直接编辑