Skip to Main Content
GitBack to Top

Git

3 minutes

定期更新 该博客

日常开发中遇到的 git场景 ,在搜索应用后,总结到这里来,便于后续积累迁移

clone - 初次拉取

单分支无历史提交式

场景描述:只获取最新,分支标签关联仓库历史提交全部舍弃

git clone --depth 1 --single-branch --branch main --no-tags \
  https://mirrors.tuna.tsinghua.edu.cn/git/llvm-project.git \
  ~/00CXX_GitRes/llvm-project
  • –depth 1 只取最新一条提交
  • –single-branch 不抓其它分支(LLVM 有几千个分支,不限制会多几百 MB)
  • –no-tags 不抓 tag(LLVM 的 tag 极多,这是体积大头之一)

好习惯

克隆完立刻把“只更新 main、永不抓 tag、自动清理“固化进本地配置:

cd ~/00CXX_GitRes/llvm-project
## 良好习惯,后续每次拉取都要针对该本地仓库的基础配置进行优先设置
## bat .git/config ##进行查看该本地仓库的配置
git config --local remote.origin.fetch '+refs/heads/main:refs/remotes/origin/main'
git config --local remote.origin.tagOpt --no-tags
git config --local fetch.prune true
git config --local fetch.pruneTags true
git config --local gc.pruneExpire now

常用别名

# 一条命令完成"取最新快照 + 丢弃本地提交/改动"
git config --local alias.update \
  '!git fetch --depth 1 --no-tags --prune origin main && git reset --hard origin/main && git log --oneline -1'

# 定期瘦身
git config --local alias.slim \
  '!git reflog expire --expire=now --all && git gc --prune=now --quiet && git count-objects -vH'

注意:Git 没有持久化的“默认浅深度“配置项(fetch.depth 不是有效键,我查了本机 git 2.55 的配置文档确认过)。
所以 –depth 1 必须每次 fetch 时显式带上——这正是 git update 别名帮你固化的事,
别直接敲裸 git pull。

后续更新:永远只有一份快照

cd ~/00CXX_GitRes/llvm-project

git update # = fetch --depth 1 + reset --hard,等价于下面两行

验证“确实没在攒历史“

# 任何时候三条命令自检:
git rev-parse --is-shallow-repository   # 必须是 true
git rev-list --count HEAD               # 必须恒等于 1
git tag | wc -l                         # 必须恒等于 0
git count-objects -vH                   # size-pack 明显膨胀时跑一次 git slim

如果 is-shallow-repository 变成 false,说明中间某次更新用了不带 –depth 的命令(或镜像不支持 shallow 而返回了全量),最省事的处理是删掉重克隆一次。
额外网络优化(可选):git config –global http.version HTTP/1.1 和 git config –global http.postBuffer 524288000 能缓解大包传输偶发卡死;如果你本机有代理(看到你装过 Geph),可以只给 GitHub 走代理、国内镜像直连

代码提交跟踪管理 - .gitignore

  • 忽略整个文件夹‌:logs/ 或 logs/**,匹配该目录及子目录下的所有文件。
  • 仅忽略文件夹内文件,保留文件夹本身‌:logs/*,不会忽略空目录,但会忽略其直接子文件和子目录。
  • 取消对已跟踪文件的忽略‌:.gitignore 只对未跟踪文件生效;若文件已提交,需先执行 git rm –cached <路径>,再提交删除记录
build/*  #cmake搭建的C++项目,编译文件不提交,但该空目录是编译构建路径,建议提交,可采用该写法
logs/** # 日志在项目运行实时产生,无需提交到仓库中进行跟踪管理

add - 添加到仓库

add .

commit - 提交

git commit -m "Update File md"

push - 推送

git push origin mian

status - 状态

git status

额外常见命令

cloc - 统计代码量

cloc . # 在当前目录下按文件类型进行代码量统计