git remote
管理远程仓库的连接,包括添加、删除、重命名和查看远程仓库
语法
git remote [<subcommand>] [<options>]
参数
| 参数 | 说明 | 示例 | 级别 |
|---|---|---|---|
-v |
显示远程仓库的 URL 详情 | |
常用 |
add <name> <url> |
添加新的远程仓库 | |
常用 |
remove <name> |
删除远程仓库 | |
常用 |
rename <old> <new> |
重命名远程仓库 | |
常用 |
set-url <name> <url> |
修改远程仓库的 URL | |
常用 |
示例
查看所有远程仓库及其 URL
git remote -v
origin https://github.com/user/repo.git (fetch)
origin https://github.com/user/repo.git (push)
添加上游仓库(fork 场景常用)
git remote add upstream https://github.com/original/repo.git
删除远程仓库连接
git remote remove origin
将远程 URL 从 HTTPS 切换为 SSH
git remote set-url origin git@github.com:user/repo.git
查看远程仓库的详细信息
git remote show origin
技巧
- fork 项目后添加 upstream 方便同步原仓库更新
- 切换 HTTPS 到 SSH 可以避免每次输入密码
- 一个本地仓库可以关联多个远程仓库
- git remote prune origin 可以清理已删除的远程分支引用