[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进去历史记录了。

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

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

    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

相关推荐
codigger8 分钟前
Git 三区域模型:把 add、commit、reset、merge 一次讲透
git·github·编程·编程语言
高频因子挖掘机8 分钟前
行情监控脚本重启后,怎样快速恢复而不重复拉取数据?
后端·github·api
水饺编程24 分钟前
第1章:开发环境搭建,在 Windows 中安装 Git
linux·c语言·汇编·git·ubuntu
idanzk29 分钟前
Git 推送 GitHub 报 SSL_READ /src refspec main 不匹配 完整踩坑记录
git·github·ssl
粥里有勺糖36 分钟前
视野修炼-技术周刊第135期 | 鼠标跟随宠物
前端·github·aigc
粥里有勺糖44 分钟前
拿 Codex 协助清理磁盘,Nice!
前端·后端·github
miofly2 小时前
GitHub 日榜趋势速报 | 2026-10-05
开源·github
m4Rk_2 小时前
【论文阅读】Agent 记忆机制(90):HyperMem——用超图建模长期记忆中的高阶关联
论文阅读·人工智能·学习·开源·github
星恒随风3 小时前
Linux开发工具详解(二):Git版本控制、GitHub协作与GDB调试实战
linux·笔记·git·学习·github
cuijiecheng20183 小时前
Windows 下使用 GitHub Private Repository 管理私人 Visual Studio/C++ 项目
windows·github·visual studio