[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

相关推荐
fanTuanye2 分钟前
Git基本使用(很详细)
git·github
忍者扔飞镖18 分钟前
git
git
Evenknow20 分钟前
将"修改源码"改为更专业的"二次开发",体现技术深度
前端·github
uhakadotcom23 分钟前
rAthena:快速入门与基础知识详解,附实用示例代码
面试·架构·github
李菠菜2 小时前
解决Windows系统下Git克隆时报错“unable to checkout working tree”的方法详解
windows·git
island13142 小时前
【git#4】分支管理 -- 知识补充
大数据·git·elasticsearch
qianmoQ3 小时前
GitHub 趋势日报 (2025年04月24日)
github
船长@Quant3 小时前
协作开发攻略:Git全面使用指南 — 引言
git·版本控制·源代码管理·协作开发
极小狐4 小时前
极狐GitLab 项目功能和权限解读
运维·git·安全·gitlab·极狐gitlab
极小狐4 小时前
极狐GitLab 如何 cherry-pick 变更?
人工智能·git·机器学习·gitlab