Git 使用经验第一弹

type
Post
date
Dec 20, 2025
summary
Git 日常使用经验记录
category
实践技巧
tags
git
password
URL
Property
Dec 17, 2025 01:18 AM

Git 历史体积瘦身

  1. 在虚拟环境中安装工具包:pip install git-filter-repo
  1. 使用工具包进行 git 历史文件分析:git filter-repo --analyze
    1. 这会在当前项目的 .git 路径下生成分析文件 .git/filter-repo/analysis , 主要查看 path-all-sizes.txt 中的内容
      执行 :head -n 30 .git/filter-repo/analysis/path-all-sizes.txt 查看大文件
  1. 删除体积较大的历史文件并回收空间
    1. git filter-repo \ --path path/to/largefile1.bin \ --path path/to/largefile2.csv \ --invert-paths git reflog expire --expire=now --all # 清空所有引用日志 (reflog) 的记录,删除对已经被移除对象 git gc --prune=now --aggressive # 垃圾回收 + 优化
  1. 对删除的内容进行确认,如果都没有输出,说明历史清理成功。
    1. git log --all --pretty=format:'' --name-only | grep 'data/input_data'
  1. 查看空间占用大小
    1. du -sh .git # 查看git仓库大小 du -sh . # 查看当前项目的大小
  1. 推送项目,强制推送(如果内部的gitlab被设置了不能强制推送则可能失败),也可以新建仓库推送
    1. git push electricity_theft_identification master --force

HTTP免密

基础免密设置

在 Windows + WSL 下配置 Git Credential Manager(GCM)来免输 HTTP 凭据
  1. 确保 Windows 端有 GCM;如果没有则重新安装最新 Git for Windows,安装过程中 勾选 “Git Credential Manager Core” 选项。
    1. git credential-manager --version
  1. 在 WSL 中配置 Git 使用 Windows 的 GCM,在 WSL 终端(如 Ubuntu)执行:
    1. git config --global credential.helper "./Git/mingw64/bin/git-credential-manager.exe"
      ./Git/mingw64/bin/git-credential-manager.exe 是GCM的可执行路径,在Windows中找到这个路径,再转换成wsl中的路径。
  1. 多个远端仓库独立设置
    1. git config --global credential.useHttpPath true
  1. 验证,预期输出第二步配置的可执行文件
    1. git config --get credential.helper
  1. 进行推送和拉取验证,只需要第一次输入用户名和密码即可

同站点重复认证

设置同一站点只需要一次账户密码输入,而不是每个仓库都需要输入一次账号密码;
Git/GCM 默认把凭据按更细粒度做了区分——常见是按 host + path(仓库路径) 存储。这样同一个 GitLab/GitHub 账号下,只要仓库路径不同,就会被当成“新的凭据条目”,于是每个新仓库都会再提示一次。
最关键的开关是:credential.useHttpPath
  • true:凭据按 host + 仓库路径 匹配(每个 repo 可能都要输入一次)
  • false:凭据按 host 匹配(同一站点只需输入一次)
  1. 确认当前的认证方案:git config --show-origin --get-all credential.helper
    1. credential.useHttpPath=true 会让 Git 在查找/保存凭据时把 仓库路径(path) 也纳入 key(相当于按 host + path 匹配),所以同一账号、同一站点,只要仓库换了路径,就会被当成“新条目”,于是每个新仓库都会再提示一次登录。
  1. 为指定站点单独设置单次认证
    1. # 按站点设置认证方式 git config --global credential.https://github.com.useHttpPath false git config --global credential.http://192.168.233.106.useHttpPath false #清掉旧的“按 path 存的”凭据条目 printf "protocol=https\nhost=github.com\n\n" | git credential reject printf "protocol=http\nhost=10.32.233.106\n\n" | git credential reject # 确认对站点的终生生效值(预期为false) git config --global --get-urlmatch credential.useHttpPath https://github.com/OWNER/REPO.git git config --global --get-urlmatch credential.useHttpPath http://192.168.233.106/GROUP/REPO.git
  1. 重新推送输入账号密码,之后同一站点下的项目不用重新认证

多远端仓库推送

git remote -v git remote remove github-Etheft-identificatie git remote remove gitlab-Etheft-identificate git remote add origin http:.../electricity_theft_identification git remote set-url --add --push origin http:.../electricity_theft_identification git remote set-url --add --push origin https:.../Electricity_Theft_Identification.git git remote -v S# 确保一个拉取源,多个推送源 origin http:.../electricity_theft_interval_detection (fetch) origin https:.../Electricity_Theft_Interval_Detection.git (push) origin http:.../electricity_theft_interval_detection (push) # 删除某个不要的推送源 git remote set-url --delete --push origin https:.../Electricity_Theft_Identification.git # 删除拉取源 git remote set-url --delete origin http:.../electricity_theft_interval_detection # 重新设置拉取源 git remote set-url origin <NEW_FETCH_URL>
 
If you have any questions, please contact me.