本文主要作为事件记录和git命令的整理

主要解决问题:

  • 本地git和远程github的连接
  • 本地仓库内的大文件无法上传github
  • 转分支的问题

本地git和远程github的连接

  1. 创建本地仓库

    git init

    ## 将文件夹下的所有文件一起添加
    git add .

    ## コメント
    git commit -m "text"

    ## 确认当前工作区已提交完整
    git status
  2. 在github建立新仓库

  3. 与新仓库建立连接

    git remote add origin <remoteAddress>

    ## 1. 每次都设置要上传的分支名
    git push -u origin main

    ## 2. (推荐)首次设置好分支后,以后只需要使用 git push
    git push --set-upstream origin main

注意:
github今年的默认分支由master改为了main,所以如果使用网上的老教程进行上传会发现github里多出来了master的分支;而默认分支main里面居然空无一物!
此时就需要将文件重新上传到main → 问题3: 转分支的问题

本地仓库内的大文件无法上传github

github默认管理文件为100MB以内,如果超出会提示:

remote: error: File base_M/epoch=014-val_loss=18.4833.ckpt is 545.33 MB; this exceeds GitHub's file size limit of 100.00 MB
remote: error: GH001: Large files detected. You may want to try Git Large File Storage - https://git-lfs.github.com.

如果决定不上传该文件了,需要进行以下操作:

  1. 删除文件
  2. 移除commit关联
  3. 重新push

删除文件

不仅要本地删除,并且要用git命令从仓库删除:否则会无法进行commit导致报错

git rm <file>
git commit -m "delete file"

移除commit关联

删除文件后重新push依旧会显示超出100MB:这是因为commit里仍留有文件信息。我们必须想办法把相关的commit删除了

  1. 最公式:回退版本

    git reset —soft HEAD~<回退数>
    ## soft:只回退commit,file状态保存

    参考:

    第6話 git reset 3種類をどこよりもわかりやすい図解で解説!【連載】マンガでわかるGit ~コマンド編~ - itstaffing エンジニアスタイル

    You just committed a large file and can’t push to GitHub | L. Collado-Torres

  2. 最简单:重新建立本地仓库

    当commit还不多时,删除本地仓库重建是最方便的

    ## 删除本地仓库
    ## -f: 无视提示
    rm -fr .git
  1. 使用外援

    对于找不到回退版本以及commit多的像屎一样的屎山,可以利用外部工具帮我们找到只和file相关的commit并删除

    Gitで不都合なファイルをコミットから削除する

重新push

这时再向github上传就顺利了

转分支的问题

使用古早教程的倒霉蛋就是我,因此成功push后却发现给我建了个新分支master??

master → main 步骤如下

  1. 修改本地仓库的分支名为main
  2. 删除本地和远程分支master
  3. 将main的内容pull下来
  4. 重新push到main

参考:

conda 导出环境/导入环境/导出base环境_conda 复制base环境-CSDN博客

修改本地仓库的分支名为main

## 查看当前的branch
git branch

## 修改名字
git branch -m master main

删除本地和远程分支master

## 删除本地
git branch -d localBranchName

## 删除远程
git push origin --delete remoteBranchName

将main的内容pull下来

在上传本地仓库之前可能在远程里可能已经保存了一些东西:如readme, 原始数据等

为保证push顺利我们必须让本地也存在这些文件

## 允许不相关文件的pull
git pull origin main --allow-unrelated-histories

## 提示
hint: You have divergent branches and need to specify how to reconcile them.
hint: You can do so by running one of the following commands sometime before
hint: your next pull:
hint:
hint: git config pull.rebase false # merge (the default strategy)
hint: git config pull.rebase true # rebase
hint: git config pull.ff only # fast-forward only

根据提示选第一种就好,即默认设置为优先远程文件,并将多出的文件merge到当下本地仓库

git config pull.rebase false

再次pull就顺利了

重新push到main

git push --set-upstream origin main