Skip to content

git remote

Managing remote 仓库使用 git remote — 添加, renaming, and 移除 upstream 连接.

你将学到

  • 如何 添加, 列表, rename, and 移除 remotes
  • 原点 and upstream 约定
  • 更改 remote URL (HTTPS 到 SSH)
  • Viewing remote 分支 and tracking

概念讲解

什么是远程仓库?

remote 是一种 named pointer to a repository hosted elsewhere — on GitHub, GitLab, a private server, or even another directory on your machine. Remotes let you push and pull changes between repositories.

git remote -v
# origin  https://github.com/user/repo.git (fetch)
# origin  https://github.com/user/repo.git (push)

Each remote has a name. 原点 is the conventional name for the main remote, automatically created when you clone.

Adding a Remote

git remote add origin https://github.com/username/repo.git
git remote add upstream https://github.com/original-author/repo.git

The upstream remote is commonly used in the forking workflow: 原点 points to your fork, upstream points to the original repository.

Listing Remotes

git remote              # list remote names
git remote -v           # list with URLs (fetch and push)
git remote show origin  # detailed info about a remote

git remote show 原点 displays:

  • The URL
  • The remote branches
  • Which local branches track remote branches
  • Whether the remote is up to date

Renaming a Remote

git remote rename origin github

This changes the remote name from 原点 to github. All tracking references update automatically.

Changing a Remote URL

A common scenario: you set up a repo with HTTPS and want to switch to SSH (to avoid password prompts):

git remote set-url origin [email protected]:username/repo.git

Or switch from SSH back to HTTPS:

git remote set-url origin https://github.com/username/repo.git

Removing a Remote

git remote remove origin

This removes the remote and all its tracking branches. Your local commits and branches are not affected.

Remote Branches

Remotes have branches too. After fetching, you can see them:

git fetch origin
git branch -r          # list remote branches
# origin/main
# origin/feature-login
# origin/develop

git branch -a          # list all branches (local + remote)

Remote branches are referenced as 原点/主要, 原点/feature-登录, etc. You cannot commit directly to them — you fetch, merge, and push.

Tracking Branches

When you clone, your local 主要 tracks 原点/主要. You can set up tracking for other branches:

git branch --set-upstream-to=origin/develop develop
# or:
git checkout -b develop origin/develop

Tracking enables git 推送 and git 拉取 without specifying the remote and branch.

Multiple Remotes

You can have multiple remotes for different purposes:

git remote add origin    https://github.com/you/repo.git      # your fork
git remote add upstream  https://github.com/team/repo.git      # original
git remote add backup    https://github.com/you/repo-backup.git # backup

Push to different remotes as needed:

git push origin main      # push to your fork
git fetch upstream        # get changes from the original
git push backup main      # push to your backup

示例代码

              # --- Set up the forking workflow with two remotes ---
git clone https://github.com/YOUR-username/project.git
cd project

# origin is already set to your fork
git remote -v

# Add the original repo as 'upstream'
git remote add upstream https://github.com/ORIGINAL-username/project.git
git remote -v
# origin    https://github.com/YOUR-username/project.git (fetch)
# origin    https://github.com/YOUR-username/project.git (push)
# upstream  https://github.com/ORIGINAL-username/project.git (fetch)
# upstream  https://github.com/ORIGINAL-username/project.git (push)

# Get changes from the original repo
git fetch upstream
git branch -r  # see upstream's branches

# Merge upstream changes into your local main
git checkout main
git merge upstream/main
git push origin main  # push the updated main to your fork

# --- Change from HTTPS to SSH ---
git remote set-url origin [email protected]:YOUR-username/project.git
git remote -v  # verify the URL changed

# --- Detailed remote info ---
git remote show origin
# * remote origin
#   Fetch URL: [email protected]:YOUR-username/project.git
#   Push  URL: [email protected]:YOUR-username/project.git
#   HEAD branch: main
#   Remote branches:
#     main     tracked
#     develop  tracked
#   Local branches configured for 'git pull':
#     main merges with remote main

# --- Rename and remove ---
git remote rename origin myfork
git remote remove backup
            

设置上 forking workflow 使用原点 and upstream remotes, 获取 and 合并从 upstream, 切换从 HTTPS 到 SSH, shows detailed remote 信息, and demonstrates renaming and 移除 remotes. 原点/upstream 约定是 标准 for 打开-源 contributions.

动手试一试

常见错误

错误写法

Confusing 原点 and upstream in forking workflow

正确做法

In forking workflow: 原点点 到 YOUR Fork (哪里你 推送), upstream 点到 ORIGINAL 仓库 (哪里你 获取更新). 你推送到 原点 and 获取从 upstream. Mixing 它们上 平均值你 要么推送到 仓库你 cannot 编写到, or 获取从 你的 own Fork 相反的 原始.

错误写法

Forgetting 到获取之前检查 out remote 分支

正确做法

Remote 分支仅 appear 之后你 获取. 运行 git 获取首先, 然后 git 分支 -r 到 see 它们, 然后 git checkout -b 局部-name 原点/remote-name 到创建局部 tracking 分支. 如果你 try 到 checkout remote 分支那个你 有不 获取, Git 将 say 它做 不 exist.

相关代码片段

相关速查表

练习 git remote

4 个练习

练习 git remote

查阅不认识的术语

新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。

查看术语表

动手做项目

通过引导式项目应用所学,含起始代码与解决方案。

查看项目

解读错误信息

用大白话解释常见错误——什么意思、为什么发生、如何修复。

查看错误