#17 Git LFS

2023-04-26

开源中国文章 紧跟 AI 步伐, Gitee 已支持 AI 模型托管 中介绍了 Git LFS 的使用。

结合 git lfs help,记录如下:

  1. 需要先 install 一下,启用 LFS 功能

    $ git lfs install
    Updated Git hooks.
    Git LFS initialized.
    
  2. 追踪大文件

    git lfs trace path/to/largefile
    # 会记录在 .gitattributes 文件中
    # path/to/largefile filter=lfs diff=lfs merge=lfs -text
    
  3. 推送大文件

    # 正常提交
    git add .gitattributes path/to/largefile
    git commit -m "..."
    
    # 推送
    git lfs push --all
    git lfs push --all origin
    
  4. 拉取大文件

    git lfs pull --all
    
    # 指定文件
    git lfs pull -I <filepath>
    
  5. git lfs ls-files 列出大文件

#16 Git 仓库迁移学习

2023-03-13

看到一篇公众号文章 《Git仓库迁移实操(附批量迁移脚本)》,介绍他们将 GitLab 中一个 Group 内的几十个项目迁移到另一个 Group。
PS:文章有提到,前提是无法得到管理员协助,开启创建时导入的功能。

  1. git clone & git push && git push --tags
  2. git clone --mirror && git push --mirror
  3. git clone --bare && git push --mirror

基本方法就是 clone && push,不过参数不同。
只是,我没有了解过这里说的 --mirror 参数,这里记录一下,用到的时候研究研究。

文章带了两个脚本:

  • Linux migrate.sh

    #!/bin/bash
    
    remote_old=git@host1:group1
    remote_new=git@host2:group2
    
    while read repo
    do
        echo $repo
        git clone --bare "$remote_old/${repo}.git"
        cd "${repo}.git"
        git push --mirror "$remote_new/${repo}.git"
        cd ..
        rm -fr "${repo}.git"
    done < repos.txt
    
  • Windows migrate.bat

    @echo off
    
    set remote_old=git@host1:group1
    set remote_new=git@host2:group2
    set input_file=repos.txt
    
    SETLOCAL DisableDelayedExpansion
    FOR /F "usebackq delims=" %%a in (`"findstr /n ^^ %input_file%"`) do (
        call :process %%a
    )
    goto :eof
    
    :process
    SETLOCAL EnableDelayedExpansion
    set "repo=!%1!"
    set "repo=!repo:*:=!"
    echo !repo!
    git clone --bare "%remote_old%/!repo!.git"
    cd "!repo!.git"
    git push --mirror "%remote_new%/!repo!.git"
    cd ..
    rmdir "!repo!.git"
    ENDLOCAL
    goto :eof
    

#13 Git 命令

2021-08-04

看到有篇文章说是 git “新增”了 switch 和 restore 两个命令,仔细一看,原来就是 2019 年就引入了的两个命令,不过我确实没有用过。

这里重新整理一下现在 git 的命令。

git version
git version 2.30.2

apt list --installed | grep ^git
git-doc/hirsute,hirsute,now 1:2.30.2-1ubuntu1 all [已安装]
git-extras/hirsute,hirsute,now 6.1.0-1 all [已安装]
git-flow/hirsute,hirsute,now 1.12.3-1 all [已安装]
git-man/hirsute,hirsute,now 1:2.30.2-1ubuntu1 all [已安装,自动]
git-svn/hirsute,hirsute,now 1:2.30.2-1ubuntu1 all [已安装]
git/hirsute,now 1:2.30.2-1ubuntu1 amd64 [已安装]
gitg/hirsute,now 3.32.1-1 amd64 [已安装]
gitk/hirsute,hirsute,now 1:2.30.2-1ubuntu1 all [已安装]

#12 使用 git push --force-with-lease 替代 git push --force

2021-07-05

今天注意到了 git push 的一个参数 --force-with-lease,可以在 Remote 有更新的时候不执行强推。
我之前考虑过会有这样的情况发生:我准备强推之前,会做最后一次拉代码检查,无误之后 force push。但是这个检查和 push 之间有一个时间差,会不会在这期间有别的小可爱提交了代码呢?
这种情况是完全可能存在的,就像是线程安全问题,只是团队的规模消减了我对这种情况的担心。
但是 --force-with-lease 参数可以彻底化解我的这种担忧,我决定以后就改用这个参数了。

#10 在 Git 历史记录中搜索

2020-06-12

搜索提交信息

git log --grep=fix: --oneline --after='2018-07-01' --author=catroll

搜索历史文件

git grep -C3 sign_position $(git rev-list --all)

搜索 diff 内容

git log -G'前置'
git log -G'前置' -p | grep '前置' -C5
git log -G'前置' --oneline --name-status

-S<string> --pickaxe-regex-G<regex> 作用相近,
不过 -S 只会列出搜索内容增删的相关信息,也就是所在行修改了,但是搜索内容没有变化的会忽略

#5 Git 补丁包

2018-08-01
  • git diff 对应 diff 命令
  • git apply 对应 patch 命令
git diff v1.2.1 v1.2.2 > v1.2.1_v1.2.2.patch

git apply --check v1.2.1_v1.2.2.patch

git apply -v --whitespace=warn v1.2.1_v1.2.2.patch

有部分文档中说 git applypatch 在一些细节上实现不一致,需要留意。但我轻量级使用,没有遇到过什么问题。

#3 使用 git-daemon

2017-03-15

有时需要临时分享一个仓库给朋友,我们可以用 SSH 协议:

git clone ssh://catroll@192.168.64.234/home/catroll/Projects/catroll/lego

其实 git-daemon 是一个更好的方法。