【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
相关推荐
a努力。3 小时前
国家电网Java面试被问:混沌工程在分布式系统中的应用
java·开发语言·数据库·git·mysql·面试·职场和发展
CoderJia程序员甲6 小时前
GitHub 热榜项目 - 日榜(2026-01-22)
ai·开源·大模型·github·ai教程
C++ 老炮儿的技术栈7 小时前
C/C++ 中 inline(内联函数)和宏定义(#define)的区别
开发语言·c++·git·算法·机器人·visual studio
CoderJia程序员甲8 小时前
GitHub 热榜项目 - 日榜(2026-01-25)
开源·大模型·llm·github·ai教程
Async Cipher9 小时前
Git 安装、配置
git
CoderJia程序员甲13 小时前
GitHub 热榜项目 - 日榜(2026-01-24)
git·ai·开源·llm·github
莫问前路漫漫13 小时前
Electerm 连接远程服务器完整指南
运维·服务器·git
鸣弦artha14 小时前
TabBar标签页组件详解
linux·git·ubuntu
TTGGGFF17 小时前
告别繁琐命令行:自研多线程 SSH 极速文件传输助手(附 GitHub 源码)
运维·ssh·github
小林up18 小时前
Ubuntu访问不了Git解决办法
linux·git·ubuntu