Git

推送Push

你可能会说

我本地改好了,怎么传到 GitHub 上让别人也能看到?

推送是把电脑上已保存的版本上传到共享代码网站的操作。例如把完成的版本上传到 GitHub,供协作或触发后续发布。它不会上传还没保存的改动;被拒绝时先查看原因并处理网上已有的变化,不要直接强行覆盖。
我的电脑多 2 个已保存版本
把新版本上传push→
网上的项目GitHub 收到更新
git push

Push 只上传已经 Commit 的版本,不会替你保存尚未提交的文件。完成后在 GitHub 检查最新版本是否出现。

组成结构 · Anatomy
git pushoriginmain
将本地分支的提交同步到远程仓库
远程仓库的代号,clone 下来的项目默认就叫 origin
指定要推送的本地分支,通常是 main 或功能分支
常见变体 · Variants
常规推送git push
git push
将本地分支提交同步到远程
首次推送push -u
git push -u origin main
首次推送时建立本地与远程分支的跟踪关系
强制推送push --force
git push --force
会重写远程分支历史,仅在明确了解影响时使用
典型使用场景
终端 git push 上云
zsh — my-first-page
$ git push Enumerating objects: 8, done. Counting objects: 100% (8/8), done. Writing objects: 100% (8/8), 1.24 KiB | 1.24 MiB/s, done. To github.com:oil-oil/my-first-page.git a3f9c21..e7b2d48 main -> main
push 被拒的现场
zsh — my-first-page
$ git push To github.com:oil-oil/my-first-page.git ! [rejected] main -> main (non-fast-forward) error: failed to push some refs hint: Updates were rejected because the remote contains hint: work that you do not have locally. hint: 'git pull' before pushing again. 别人先推了 → 先 git pull 合回来,再 push
GitHub 上确认新 commit
my-first-page 刚刚刷新
📄 index.html修复按钮在手机上错位1 分钟前
📄 style.css修复按钮在手机上错位1 分钟前
📄 .gitignore初始化项目3 天前
✓ 最新提交已同步到远程仓库
.gitignore 挡住 .env
本地文件夹
📄 index.html
📄 style.css
📄 .gitignore
📄 .env 🔑
GitHub 仓库
📄 index.html
📄 style.css
📄 .gitignore
没有 .env ✓
延伸阅读 · 权威出处