博客 / 详情

Github Push

2026.03.25
笔记摘要
1407 Words
- Views
- Comments

Github push 笔记.

1️⃣ 进入目录

Terminal window
cd /<project>

2️⃣ 删除旧的 Git 记录(关键)

  • 如果有就执行
Terminal window
rm -rf .git

3️⃣ 重新初始化

Terminal window
git init

4️⃣ 添加文件

Terminal window
git add .

5️⃣ 提交

Terminal window
git commit -m "提交备注"

6️⃣ 绑定 GitHub 仓库

Terminal window
git remote add origin https://github.com/<username>/<repositoriesname>.git

7️⃣ 推送

Terminal window
git branch -M main
git push -u origin main
  • 随后输入 github用户名 github token,注意不是密码是token 需要去申请

🚀 Github Token 生成

  1. 生成 Token

🏷️ https://github.com/settings/tokens

点击:Generate new token (classic)

  1. 权限勾选

👉 勾这个就够:repo

  1. 生成后复制

👉 会得到一个类似:ghp_xxxxxxxxxxxxxxxxx

⚠️ 注意:只显示一次!

问题集

error: failed to push some refs to 'https://github.com/<username>/<repositoriesname>.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

方案一:先拉再推(最安全)

Terminal window
git pull origin main --rebase
git push origin main

👉 推荐用 —rebase,提交历史更干净

如果出现冲突:

Terminal window
# 修改冲突文件
git add .
git rebase --continue
git push origin main

方案二:强制覆盖远程(危险但干脆) 如果你确定:❗远程代码不重要(比如刚创建的仓库)

直接:

Terminal window
git push -f origin main

👉 会覆盖 GitHub 上所有内容