[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

相关推荐
u13013018 分钟前
GitHub 热榜项目:日榜(2026-09-05)
github
m4Rk_34 分钟前
【论文阅读】Agent 记忆机制(61):CFGM——用粗到细的记忆落地贯通经验采集、知识蒸馏与在线纠错
论文阅读·人工智能·学习·开源·github
是2的10次方啊1 小时前
GitNexus:给 AI 代理装上代码知识图谱
github
其实防守也摸鱼2 小时前
OpenClaw 深度解析:从原理到实战的完整指南
运维·服务器·数据库·github·copilot
笨笨饿2 小时前
#137_死机恢复现场_Armino平台AP系统swd调试
git·python·stm32·单片机·嵌入式硬件·物联网·vim
King of fraud3 小时前
Git 入门:从零开始的版本控制之旅
git
AI大模型-小华3 小时前
Codex CLI第一次怎么用?从安装到读取本地项目完整教程
git·node.js·ai编程·开发工具·代码分析·codex·codex cli
峰向AI3 小时前
产品经理神器:PRD 直接变可点击网页!
github
2401_861678623 小时前
Git 在 Windows 使用
windows·git·elasticsearch
Rescenix3 小时前
流式瀑布渐变闪烁根治-硬核技术版
架构·github