CodeRoadMap
首页路线题库资源库文章社区
CodeRoadMap

程序员的学习成长路线图。内容免费开放,登录后可同步路线图与课时进度。

由 程序员柏风 独立维护 · 关于作者

学习

  • 路线图
  • 课程
  • 面试题
  • 文章
  • 我

更多

  • 资源
  • 社区
  • 关于作者
  • 内容说明

© 2026 CodeRoadMap

津ICP备2026012044号-1·津公网安备12011002026155号·coderoadmap@126.com
Git 版本控制入门/课时/远程仓库与 push/pull
RawBlame
课时目录8 节

Git 版本控制入门

  1. 01版本控制与 Git 是什么
  2. 02安装 Git 与首次配置
  3. 03创建仓库与第一次提交
  4. 04状态查看与提交历史
  5. 05分支创建与切换
  6. 06合并分支与冲突处理
  7. 07远程仓库与 push/pull
  8. 08日常协作工作流小结

远程仓库与 push/pull

25 分钟

远程仓库与 push / pull

本地 Git 仓库可以和 GitHub、GitLab 等**远程(remote)**同步。push 上传你的 commit,pull 拉取别人的更新。

学习目标

  • 添加 remote、首次 push
  • 理解 origin、main 与 upstream 跟踪分支
  • 克隆已有仓库 git clone

远程是什么

远程是保存在服务器上的同名 Git 仓库。常见命名:

  • origin — 默认远程名,指向你 fork 或 push 的主仓库

查看:

git remote -v

从本地关联 GitHub 新仓库

在 GitHub 网页创建空仓库(不要勾选 README 若本地已有 commit),然后:

git remote add origin git@github.com:YOUR_USER/git-practice-hello.git
# 或 HTTPS:
# git remote add origin https://github.com/YOUR_USER/git-practice-hello.git

git push -u origin main

-u 设置 upstream,之后在该分支可直接 git push / git pull。

克隆已有项目

git clone https://github.com/YOUR_USER/hello-git.git
cd hello-git
git remote -v

克隆会下载完整历史并自动配置 origin。

日常同步

# 拉取远程 main 并合并到当前分支
git pull origin main
# 已设 upstream 时:
git pull

# 推送本地 commit
git push

先 pull 再 push 是习惯:避免在过期的 main 上 push 被拒绝。

SSH 与 HTTPS

方式优点注意
HTTPS配置简单需 token,勿把密码写进 URL
SSH免重复输 token本机配置 ssh-key 并加到 GitHub

配置好 SSH 后,地址形如:git@github.com:YOUR_USER/your-repo.git。

常见报错

rejected — non-fast-forward
→ 远程有你本地没有的 commit,先 git pull --rebase 或 git pull 再 push。

Permission denied (publickey)
→ SSH 密钥未配置或未加到 GitHub。

Large files
→ 勿 push 大二进制;用 .gitignore。Git LFS 为进阶话题。

与路线图 GitHub 节点

全栈路线图 GitHub 节点对应:Issues、Pull Request、Actions 等。本课掌握 push/pull 后,可在 GitHub 上:

  • 开 PR 请他人 review
  • 用 Actions 跑 CI 自动测试

练习

  • 在 GitHub 创建练习仓库并完成首次 push
  • 在另一目录 clone 下来,改一行后 push
  • 执行 git remote -v 确认 origin 地址

小结

remote 是协作中心;push 分享、pull 更新。 下一课串起分支 + 远程的完整日常流程。

加载本节练习…
← 上一课合并分支与冲突处理下一课 →日常协作工作流小结