【git】github 如何同步别人的仓库

【git】github 如何同步别人的仓库

前言

假设你有两个 Git 仓库,并希望同步它们,以便它们含有相同的内容。

你必须要在 Git 中配置一个远程服务器指向上游的仓库地址,这样你在 fork 中的更改才能同步到原始的仓库里。这样也能把原始仓库中的变更同步到 fork 里。

第 1 步

打开终端,进入本地项目的工作目录。

第 2 步

查看你的 fork 当前配置的远程仓库地址:

bash 复制代码
$ git remote -v
 origin https://github.com/YOUR_USERNAME/YOUR_FORK.git (fetch)
 origin https://github.com/YOUR_USERNAME/YOUR_FORK.git (push)

第 3 步

指定当前 fork 将要同步的上游远程仓库地址:

bash 复制代码
$ git remote add upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git

第 4 步

验证一下你刚指定的上游仓库地址:

bash 复制代码
$ git remote -v
 origin https://github.com/YOUR_USERNAME/YOUR_FORK.git (fetch)
 origin https://github.com/YOUR_USERNAME/YOUR_FORK.git (push)
 upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git (fetch)
 upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git (push)

第 5 步

从上游仓库拉取分支及其对应的提交记录。对于 master 的提交会保存在一个本地分支 upstream/master 里。

bash 复制代码
$ git fetch upstream
 remote: Counting objects: 75, done.
 remote: Compressing objects: 100% (53/53), done.
 remote: Total 62 (delta 27), reused 44 (delta 9)
 Unpacking objects: 100% (62/62), done.
 From https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY
 * [new branch] master -> upstream/master

第 6 步

签出你的 fork 的本地 master 分支

bash 复制代码
$ git checkout master
 Switched to branch 'master'

第 7 步

把 upstream/master 中的变更合并到本地的 master 分支里。这样你的 fork 的 master 分支就和上游仓库同步了,也不会丢失本地的更改。

bash 复制代码
$ git merge upstream/master
 Updating a422352..5fdff0f
 Fast-forward
 README | 9 --- --- --- -
 README.md | 7 ++++++
 2 files changed, 7 insertions(+), 9 deletions(-)
 delete mode 100644 README
 create mode 100644 README.md

第 8 步

把更改推送到服务器:

bash 复制代码
$ git push
相关推荐
安河桥畔7 分钟前
Git使用
大数据·git·elasticsearch
会跑步的蜗牛10 分钟前
git相关问题(个人记录)
git
exm-zem10 分钟前
Git 常用指令
git
映秀小子12 分钟前
git设置命令缩写
git
Dream_sky分享13 分钟前
IDEA 2025中git的Local Changes找不到
java·git·intellij-idea
笑鸿的学习笔记1 小时前
git笔记之清理本地存在但远程不存在的分支
笔记·git
和小潘一起学AI1 小时前
git、gitee
git
鱼香rose__1 小时前
git的基本使用
linux·git
无限进步_14 小时前
【C++】大数相加算法详解:从字符串加法到内存布局的思考
开发语言·c++·windows·git·算法·github·visual studio
IT WorryFree16 小时前
Git for Windows v2.52.0 发布说明
windows·git