大家好,我是程序员柏风,AI 最全的 Vibecoding 学习地图 http://coderoadmap.cn 作者。
远程仓库与 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 更新。 下一课串起分支 + 远程的完整日常流程。