Clone空仓库后首次上传文件

场景:GitHub/Gitee上新建了一个空仓库,clone到本地后往里加文件、首次push

完整流程

bash cd 仓库名 # 1. 进入目录 git add . # 2. 添加到暂存区 git commit -m "初始提交" # 3. 提交到本地仓库 git push -u origin main # 4. 推送并设置upstream(第一次需要 -u) ​

关键点

  • -u / --set-upstream:只有第一次push需要,作用是关联本地分支和远程分支
    • 设置一次后,之后直接 git push / git pull 即可,不用再写 origin main
  • 分支名注意:新版本 Git 默认是 main,老仓库可能是 master,不确定用 git branch 查看当前分支

踩过的坑 / 报错记录

fatal: The current branch main has no upstream branch. ​ → 原因:本地分支没有关联远程分支 → 解决:git push --set-upstream origin main

相关命令速查

命令作用
git remote -v查看远程仓库地址
git branch查看当前分支
git push -u origin main首次推送并关联upstream

延伸阅读 / 关联笔记