[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

相关推荐
Moment1 小时前
MinIO已死,MinIO万岁
前端·后端·github
草梅友仁6 小时前
OpenClaw AI 助手实测与墨梅博客更新 | 2026 年第 10 周草梅周报
开源·github·ai编程
ExcellentWe1 天前
你的AI写的代码总是不理想?这个开源免费的工程流水线编排工具super-dev帮你解决
github
Moment1 天前
腾讯终于对个人开放了,5 分钟在 QQ 里养一只「真能干活」的 AI 😍😍😍
前端·后端·github
小兵张健2 天前
开源 playwright-pool 会话池来了
前端·javascript·github
goodspeed2 天前
Git Worktree:多分支并行开发的利器
git·github
Moment3 天前
Vibe Coding 时代,到底该选什么样的工具来提升效率❓❓❓
前端·后端·github
雮尘3 天前
让 AI Agent 高效并行开发的命令-git worktree
人工智能·git·agent
有道AI情报局4 天前
网易有道龙虾 NAS 服务器部署与实战指南
github