[git操作] git创建仓库上传github报错

操作流程如下

  1. 使用 git init
  2. 使用 git remote add origin 项目ssh链接
  3. git add .

报错如下

bash 复制代码
Bus error (core dumped)

然后执行任何别的操作都会报错:

复制代码
fatal: Unable to create 'path .. /.git/index.lock': File exists.

Another git process seems to be running in this repository, e.g.
an editor opened by 'git commit'. Please make sure all processes
are terminated then try again. If it still fails, a git process
may have crashed in this repository earlier:
remove the file manually to continue.

输入git status 查看,之前的add操作确实没有成功:

复制代码
On branch master

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        Checkpoints/
        Code/
        Data/
        Misc/
        Results/
        Scripts/

nothing added to commit but untracked files present (use "git add" to track)

按照它的提示,把 .git/index.lock 文件手动删掉,然后重新 git add . 就可以了。

修改本地master成为main

然后拉取remote的仓库

复制代码
git pull origin main

如果是新建仓库,可能会报错:efusing to merge unrelated histories这通常是因为本地分支上的代码和远程分支上的代码没有共同祖先,也就是说他们之间没有关联。要强制合并两个不相关的历史记录:

复制代码
git pull origin main --allow-unrelated-histories

pull之后merge,merge之后commit修改

复制代码
git commit -m  "your commit message"

push的方式

复制代码
git push --set-upstream origin main # 如果第一次git push失败了,可以这么干

git限制了100MB的大小

所以注意要在 .gitignore 文件处设置好,不要上传大的文件

比如深度学习模型实验中需要忽略的文件,注意不要多加 ./ ,比如 ./Data/* 它还是会上传 Data 的!!!

复制代码
# data files can't upload
**/__pycache__
Data/NpyData
Data/cuda_normalizers
Data/vahadane_images
Checkpoints
Results
wandb
Code/models/backbones/pretrained_weight
  • 首先使用 git count-objects -vH 查看项目git历史的大小

  • 然后使用这个命令找到比较大的文件: git rev-list --all | xargs -rL1 git ls-tree -r --long | sort -uk3 | sort -rnk4 | head -10

列出所有仓库中的对象(包括SHA值、大小、路径等),并按照大小降序排列,列出TOP 10。

可以看到确实有一些文件已经add进去历史记录了。

所以我们需要修改历史记录。

  • 使用这两个命令删除 ResultsCheckpoints 文件夹下面的数据(历史版本)

    git filter-branch --index-filter 'git rm --cached --ignore-unmatch Results/' -- --all
    git filter-branch --index-filter 'git rm --cached --ignore-unmatch Checkpoints/
    ' -- --all

然后使用这几个命令:

复制代码
rm -Rf .git/refs/original # 删除git的备份
rm -Rf .git/logs/ # 删除logs
git gc # 收集所有松散对象并将它们存入 packfile(垃圾回收)
git prune # 删除所有过期的、不可达的且未被打包的松散对象
  • 因为修改过历史,需要 force 上传,这也是最不好的一点,个人项目无所谓了。

    git push origin main --force

    上传到远程目标分支 git push origin target_branch_in_remote --force

相关推荐
xuhe23 小时前
[全流程详细教程]Docker部署ClawBot, 使用GLM4.7, 接入TG Bot实现私人助理. 解决Docker Openclaw Permission Denied问题
linux·docker·ai·github·tldr
先跑起来再说3 小时前
Git 入门到实战:一篇搞懂安装、命令、远程仓库与 IDEA 集成
ide·git·后端·elasticsearch·golang·intellij-idea
宇宙帅猴4 小时前
GitHub 私有仓库认证完整指南:告别密码错误,使用 PAT 令牌
github
前端市界6 小时前
用 React 手搓一个 3D 翻页书籍组件,呼吸海浪式翻页,交互体验带感!
前端·架构·github
happyprince7 小时前
2026年02月07日热门github项目
github
承渊政道7 小时前
Linux系统学习【Linux系统的进度条实现、版本控制器git和调试器gdb介绍】
linux·开发语言·笔记·git·学习·gitee
Doro再努力7 小时前
【Linux操作系统12】Git版本控制与GDB调试:从入门到实践
linux·运维·服务器·git·vim
CoderJia程序员甲8 小时前
GitHub 热榜项目 - 日榜(2026-02-06)
人工智能·ai·大模型·github·ai教程
荔枝吻9 小时前
忘记服务器密码,在Xshell7中查看已保存密码
运维·服务器·github
摇滚侠9 小时前
MAC IDEA GIT 提交区显示了几个不存在的目录
git·idea