Git 代码上传至 GitHub 完整指南
快速引导
# 初始化 Git 仓库(如果尚未初始化)
git init
# 将本地仓库与远程仓库关联
git remote add origin https://github.com/your-username/your-repository.git
# 添加文件到暂存区
git add .
# 提交更改
git commit -m "Initial commit"
# 推送代码到远程仓库(适用于 main 分支)
git push -u origin main
# 后续上传单个文件
git pull origin main
# 将特定文件的更改添加到暂存区
git add <你要更新的文件名>
# 或者,添加所有更改的文件(包括你修改的那个)
# git add .
# 将更改正式提交,并附上清晰的说明
git commit -m "更新说明:例如'修复了 XX 功能的 Bug'"
git push origin main
# or 如果想强制推送到远程仓库(覆盖所有内容)
git push -f origin main
准备工作
1. 注册 GitHub 账号
访问 github.com 点击'Sign up'注册一个新账号。建议:
- 使用专业化的用户名(与你的技术身份相关)
- 验证邮箱地址,确保账户安全
2. 安装 Git 工具
Git 是 GitHub 的底层版本控制系统,你需要先在本地安装。
3. 配置 Git 身份信息
打开终端(或 Git Bash)配置你的身份信息:注册 GitHub 时用到的信息
git config --global user.name "你的用户名"
git config --global user.email "你的邮箱"
这些信息将记录在你的每一次提交中。
核心操作:上传代码到 GitHub
方式一:从零开始创建新仓库
步骤 1:在 GitHub 上创建仓库
- 登录 GitHub,点击右上角'+'图标,选择'New repository'。
- 填写仓库信息:
- Repository name: 仓库名称(建议使用小写字母和连字符)
- Description: 项目描述(可选但建议填写)
- Public/Private: 选择公开或私有
- Initialize this repository with: 建议不要勾选,我们从本地初始化
步骤 2:本地初始化 Git 仓库
进入你要上传的文件夹,右击点击 Open Git Bash here 接着在 bash 输入,创建一个初始文件夹来存放。
git init
步骤 3:连接远程仓库
将本地仓库与 GitHub 上的远程仓库关联:地址从 GitHub 新建仓库那里复制过来就行。
git remote add origin https://github.com/你的用户名/仓库名.git
步骤 4:添加并提交代码
# 添加所有文件到暂存区
git add .
# 提交更改,附上有意义的提交信息
git commit -m "初次提交:项目初始化"
步骤 5:推送到 GitHub
# 首次推送需要设置上游分支
git push -u origin main
# 后续推送只需 git push
已经上传成功了。
方式二:上传现有项目到新仓库
如果你已经有一个正在开发的项目:
cd existing-project
git init
git add .
git commit -m "初始提交:上传现有项目"
git branch -M main #将当前分支重命名为 main(可选)
git remote add origin https://github.com/username/repo-name.git
git push -u origin main
常见问题解决
将本地项目作为子目录上传到已有的远程仓库
保留远程仓库已有内容,同时将本地项目作为子目录上传。
# 创建子目录并移动本地项目
mkdir new-subdirectory
mv * new-subdirectory
mv .* new-subdirectory 2>/dev/null
# 拉取远程仓库,允许不同历史合并
git pull origin main --allow-unrelated-histories
# 提交更改
git add .
git commit -m "Add new project as subdirectory"
git push -u origin main
解决 Git 报错:fatal: detected dubious ownership in repository at
git add . 后,报错:fatal: detected dubious ownership in repository at 这是因为该项目的所有者与现在的用户不一致。比如说:该项目的所有者是 Administrator,而当前用户是 qiuye,那么就会导致上面的错误。
git config --global --add safe.directory "*"
Git:错误:src refspec main does not match any
- 本地分支没有名为
main的分支,或者没有任何提交记录。
# 查看当前分支
git branch
# 如果当前分支是 master,重命名为 main
git branch -m master main
# 提交更改
git add .
git commit -m "Initial commit"
# 推送到远程 main 分支
git push -u origin main
# 或者将 master 分支推送到 main
git push -u origin master:main
Git 报错 'fatal: unable to access 'https://github.com/.../.git': Recv failure Connection was rese'
在终端输入以下命令,设置 Git 使用本地代理:
git config --global http.proxy http://127.0.0.1:7890
git push 出现 fatal: the remote end hung up unexpectedly Everything up-to-date
如果你之前没有提交过文件,而你在 git push 的时候出现 Everything up-to-date,并且文件也没有提交上去。可能是因为你没有 git add 和 git commit。 需要重新执行:
git add .
git commit -m "message"
git push origin master
大文件超了 GitHub 的 100MB 单文件限制
# 清理并重新推送
git gc --aggressive --prune=now
git repack -a -d --depth=250 --window=250
# 分段推送大提交
git push --no-thin origin master
解决方案:从本次提交中移除大文件
# 1. 重置到推送前的状态(保留本地修改)
git reset HEAD~1 --soft
# 2. 从暂存区移除大文件
git rm --cached best-sts-b-model.pt #大文件 1
git rm --cached models/bert-base-chinese/pytorch_model.bin #大文件 2
# 3. 重新提交(不带大文件)
git commit -m "Remove large model files exceeding GitHub limit"
# 4. 重新推送
git push origin master
长期解决方案:使用 Git LFS
Git LFS 是 GitHub 推荐的大文件管理工具。
# 1. 安装 Git LFS
# 下载地址:https://git-lfs.github.com/
# 2. 初始化 Git LFS(每个用户只需要做一次)
git lfs install
# 3. 跟踪大文件类型
git lfs track "*.pt"
git lfs track "*.bin"
git lfs track "models/**"
# 4. 查看生成的 .gitattributes 文件
cat .gitattributes
# 5. 提交 .gitattributes
git add .gitattributes
git commit -m "Add Git LFS tracking"
# 6. 重新添加大文件到 LFS
git add best-sts-b-model.pt #大文件 1
git add models/bert-base-chinese/pytorch_model.bin #大文件 2
git commit -m "Add model files via Git LFS"
# 7. 推送(LFS 会拦截处理大文件)
git push origin master
修改 GitHub 默认分支
去 setting 里面 change。
删除远程仓库中的多余文件夹或文件
# 删除本地不需要的文件或文件夹
rm -r path/to/folder
# 更新 Git 索引
git add -u
git commit -m "Remove unnecessary folder or file"
# 推送到远程仓库
git push
方法二:直接在 GitHub 界面删除
- 进入 GitHub 仓库。
- 找到需要删除的文件或文件夹。
- 点击右上角的垃圾桶图标删除。
- 提交删除更改。
总结
Git 常用命令速查表
| 场景 | 命令 |
|---|---|
| 初始化仓库 | git init |
| 关联远程仓库 | git remote add origin |
| 检查远程仓库 | git remote -v |
| 添加文件到暂存区 | git add . 或 git add |
| 提交代码 | git commit -m "message" |
| 拉取远程代码 | git pull origin main |
| 推送代码到远程仓库 | git push origin main |
| 强制推送 | git push --force |
| 检查当前分支 | git branch |
| 查看状态 | git status |
| 解决合并冲突 | git merge --abort 或手动修改冲突文件 |
| 删除远程文件 | rm -r path && git add -u && git commit |
Git 只是开始,真正的价值在于版本控制、协作开发和代码展示。养成频繁提交的习惯,让你的开发过程有迹可循。


