去除GIT某个时间之前的提交日志

背景

有时git提交了太多有些较早之前的提交日志,不想在git log看到,想把他删除掉。

方法

大概思路是通过 git clone --depth 来克隆到指定提交的代码,此时再早之前的日志是没有的

然后提交到新仓库

#!/bin/bash
ori_git="git@your.git.path/ori-prj.git"   #源仓库
ori_branch="master" 
ori_path="ori-prj"


dest_git="git@your.git.path/dest-prj.git" #需要提交到变更后的仓库
dest_branch="master"
dest_path="dest-prj"
previous_commit="41059bbcc349de4faccf484326a94d3c816cdd05" #从哪个id开始拉,不包含当前id

if [ -d "$ori_path" ]; then
    echo "$ori_path exist"
else
   echo "git clone $ori_git --branch=$ori_branch $dest_path"
   git clone $ori_git --branch=$ori_branch $ori_path
fi

cd $ori_path
git pull
#判断分支是否存在, 没有的话拉一下
if test -z "$(git branch |grep $ori_branch)"; then
  git checkout remotes/origin/$ori_branch -b $ori_branch
fi
git checkout $ori_branch

# 获取id对应的深度
current_commit=$(git rev-parse HEAD)
commit_count=$(git log --oneline $previous_commit..$current_commit | wc -l)

echo $commit_count

cd ..
#只克隆到指定id的提交记录
rm -rf $dest_path
git clone $ori_git --depth=$commit_count  --branch=$ori_branch $dest_path
cd $dest_path
git filter-branch -- --all # 要执行下这命令,不然推不上去
git remote rename origin old-origin
git remote add origin $dest_git
git push origin $ori_branch:$dest_branch --force
echo $ori_branch:$dest_branch
cd ..

相关推荐

  1. 去除GIT某个时间之前提交

    2024-01-18 13:44:06       53 阅读
  2. mysql和Oracle 查询某个时间之内数据

    2024-01-18 13:44:06       66 阅读
  3. Git 中把文件恢复到之前提交

    2024-01-18 13:44:06       26 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-18 13:44:06       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-18 13:44:06       106 阅读
  3. 在Django里面运行非项目文件

    2024-01-18 13:44:06       87 阅读
  4. Python语言-面向对象

    2024-01-18 13:44:06       96 阅读

热门阅读

  1. ElasticSearch设置用户名密码访问

    2024-01-18 13:44:06       50 阅读
  2. vue2上传图片image-conversion压缩

    2024-01-18 13:44:06       53 阅读
  3. 2. FPGA的电路结构概述

    2024-01-18 13:44:06       57 阅读
  4. AI的能力上限是使用者的认知

    2024-01-18 13:44:06       57 阅读
  5. Django的后台认证登录系统,登录多久之后失效呢

    2024-01-18 13:44:06       63 阅读