Git LFS

在Github提交大文件时会遇到文件大小限制,你可以使用Git LFS解决这个问题。

Git LFS官网: https://git-lfs.com/

或者你也可以使用Sourcetree,这是一个Git客户端,集成了Git LFS。

官方教程参考:https://www.atlassian.com/git/tutorials/git-lfs

创建Git LFS仓库

创建仓库后运行 git lfs install

1
$ git lfs install

这将在你的仓库中安装一个特殊的 pre-push Git 钩子,该钩子将在你执行 git push 的时候传输 Git LFS 文件到服务器上。

使用 Git LFS 跟踪文件

在向仓库添加新的大型文件之前,需要指定文件类型。

比如我想往我的仓库内丢一个checkpoint,那么我可以指定以下文件:.pth

  • 在任何目录中追踪所有pth文件:

    1
    $ git lfs track "*.pth"

    pth周围的引号” “很重要,不要忘了,省略将导致当前目录下为每个pth文件创建单独条目

  • 在任何目录中跟踪名为 Yuuka.pth 的文件

    1
    $ git lfs track "Yuuka.pth"
  • models目录和所有子目录中跟踪所有文件

    1
    $ git lfs track "models/"
  • 仅在 models 目录中跟踪所有文件,而不是子目录

    1
    $ git lfs track "models/*"
  • models/Yuuka 目录中跟踪所有 pth文件

    1
    $ git lfs track "models/Yuuka/*.pth"
  • 在任何名为 models的目录中跟踪所有pth文件

    1
    $ git lfs track "**/models/*.pth"

删除 Git LFS 文件

Git LFS 命令行客户端不支持删除服务器上的文件,因此如何删除他们取决于你的托管服务提供商。

在SourceTree中使用LFS

  • Tools -> Options -> Git and press Update Embedded
  • 勾选Enable the Bitbucket LFS Media Adapter
  • Repository -> Git LFS -> Initialize Repository
  • Repository -> Git LFS -> Track / untrack files … 将需要包含的大文件的后缀添加到其中。

Git 指定分支

安装正常操作clone仓库后默认是把main分支的内容拉取下来,如果想要拉取其他分支,应在git clone指定中添加如下命令行选项:

1
$ git clone -b 分支名称 url

单杠和双杠的区别:

-后面跟着的是单字符参数,可以设置多个参数在横线后面,如:

1
tar -xcvf ×××

--后面跟着的是字符串参数,只能选择一个参数放在--后面,如:

1
tar --help

在选项需要加参数的时候,可以使用如下格式实现:

1
2
git branch --set-upstream-to origin/remote_branch  your_branch
git branch --set-upstream-to=origin/remote_branch your_branch

问题收集

“The remote disconnected. Check your Internet connection and try again”

https://github.com/desktop/desktop/issues/4746

更改缓冲区大小或者使用Git LFS

1
$ git config --global http.postBuffer 1048576000

“The push operation includes a file which exceeds GitHub’s file size restriction of 100MB.Please remove the file from history and try again.”

大文件已经提交,然而在本地又被删除:Can’t push to GitHub because of large file which I already deleted

  1. 备份仓库

  2. Git GUI工具,确定包含大文件的提交。

  3. 使用以下命令

    🚨警告:

    这将删除该文件的所有历史记录。问题在于该文件仍然存在于历史记录中。

    此命令更改您的提交哈希值,这可能是一个真正的问题,特别是在共享存储库上。未经理解后果,不应执行此操作。

    1
    git filter-branch --tree-filter 'rm -f largefile.zip' HEAD

或者使用git filter-repo

🚨警告:

git-filter-branch具有大量的陷阱,会生成损坏的历史记录。在继续之前,按Ctrl-C中止,然后使用另一种过滤工具,如“git filter-repo”(https://github.com/newren/git-filter-repo/)。有关详细信息,请参见filter-branch手册页;要消除此警告,请将FILTER_BRANCH_SQUELCH_WARNING = 1设置为True。

  1. 安装git filter-repo

    1
    $ git git-filter-repo install 
  2. 要删除具有路径前缀example/path/to/something的任何文件,您可以运行

    1
    git filter-repo --path example/path/to/something--invert-paths 
  3. 要删除没有路径前缀example/path/to/something的任何文件,您可以运行

    git filter-repo –path example/path/to/something

“How to resolve a Merge Conflict in GitHub Desktop”

解决Merge冲突

https://www.youtube.com/watch?v=MIVW0sijSjY