Git 多远程仓库管理

操作步骤:

1. 查看当前的远程仓库配置

复制代码
git remote -v

你会看到类似这样的输出(origin 指向原始仓库):

复制代码
origin  https://github.com/原作者/原仓库.git (fetch)
origin  https://github.com/原作者/原仓库.git (push)

2. 重命名原始仓库为 upstream

复制代码
git remote rename origin upstream

3. 添加你的个人仓库作为新的 origin

复制代码
git remote add origin https://github.com/你的用户名/你的新仓库.git

4. 推送你的修改到你的仓库

复制代码
# 推送主分支(通常是 main 或 master)
git push -u origin master
# 或者
git push -u origin main

5. 保持与原始仓库的同步

以后当你想要同步原始仓库的更新时:

复制代码
# 从原始仓库拉取最新代码
git fetch upstream

# 合并更新到你的本地分支
git checkout main  # 或 master
git merge upstream/master

# 推送更新到你的仓库
git push origin master

完整的配置结果:

复制代码
# 查看最终的远程仓库配置
git remote -v

输出应该是:

复制代码
origin   https://github.com/你的用户名/你的新仓库.git (fetch)
origin   https://github.com/你的用户名/你的新仓库.git (push)
upstream https://github.com/原作者/原仓库.git (fetch)
upstream https://github.com/原作者/原仓库.git (push)

常用工作流程:

  1. 开发新功能:在本地修改代码

  2. 提交修改git add . && git commit -m "你的修改说明"

  3. 推送到你的仓库git push origin master

  4. 获取原始仓库更新git fetch upstream

  5. 合并更新git merge upstream/master

  6. 解决冲突(如果有)

  7. 推送合并后的代码git push origin master

这样你既能将修改推送到自己的仓库,又能随时获取原始仓库的最新更新。

相关推荐
深海鱼在掘金4 天前
Git 完全指南 —— 第1章:Git 概览与版本控制演进
git
noravinsc4 天前
关于Git Flow
git
蜜獾云4 天前
在Git中配置用户名和密码
git
scx_link4 天前
通过git bash在本地创建分支,并推送到远程仓库中
开发语言·git·bash
南大白4 天前
IntelliJ IDEA 运行时的 JVM 本地内存溢出崩溃
git
码农小旋风5 天前
Claude Code 基础用法大全:对话、分析、修改、测试、Git 和工作流
人工智能·git·chatgpt·claude
南大白5 天前
Git 撤回提交完整方案
git
像风一样的男人@5 天前
python --实现代理服务器
git·ui
sbjdhjd5 天前
从零搭建企业级 CI/CD(下):Jenkins+GitLab+Harbor 全链路实战指南
git·servlet·ci/cd·云原生·云计算·gitlab·jenkins
码云数智-大飞5 天前
Go Channel 详解:并发通信的正确姿势
前端·数据库·git