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

背景

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

方法

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

然后提交到新仓库

#!/bin/bash
ori_git="[email protected]/ori-prj.git"   #源仓库
ori_branch="master" 
ori_path="ori-prj"


dest_git="[email protected]/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 ..

你可能感兴趣的:(git)