git高级功能

git
git submodule

删除历史提交里的文件

pip install git-filter-repo
git filter-repo --path public/favicon.ico --invert-paths --force
git push origin --force

前言

为了安全或者等情况

一个子仓库一个账号,分别授予主仓库和子仓库权限

在部署服务器上就使用对应账号去拉取

Tip

.gitmodules中的url要修改成相对路径

gitlab实现方式

以下不包括账号权限

第 1 步

新增gitlab runner

第 2 步

在主仓库新增.gitlab-ci.yml逻辑

第 3 步

在主仓库新增流水线触发(WebHook)

第 4 步

在子仓库分别增加钩子

.gitlab-ci.yml
stages:
  - update

update_submodules:
  stage: update
  only:
    - triggers
  tags:
    - docker
    - ci
  image: docker:git
  variables:
    GIT_SUBMODULE_STRATEGY: recursive
  before_script:
    - git config --global user.name "gitlab bot"
    - git config --global user.email "gitlab-bot@tiho-mobi.com"
    - git remote set-url origin https://gitlab-ci-token:${GIT_PUSH_TOKEN}@gitlab.test.com/test.git
  script:
    - |
      git submodule update --remote --merge
      git add .
      if git diff-index --quiet HEAD; then
        echo "Nothing changed"
      else
        git commit -m "Auto update submodules"
        git push origin HEAD:master
      fi

本地克隆

首次

git clone https://dc-cn-sy:xxx@gitlab.xxx.com/xxx.git
git submodule init
git submodule update --remote deploy/${ENV}

后续

git pull && git submodule update --remote deploy/${ENV}

添加子仓库

git submodule add https://dc-cn-sy:xxx@gitlab.tmofamily.com/xxx.git deploy/${ENV}