写了个脚本,可以帮我们把 Github 仓库的指定分支的提交历史删除,并且以新的 username 和 email 提交全部代码,最终变为只有一次 commit 的 Github 仓库。适合帮我们去除一些隐私信息!
首先我们需要本地与远程Github进行关联,正常执行 git push 命令。
接下来我们新建一个 gitclean.sh 文本文件,并复制以下代码:
Shell
#!/bin/bash
date
reps=$1
branch=
if [ $2 ]; then
branch="-b ${2}"
fi
username=jack
author=Jack
email=Jack@email.com
array=(${reps//,/ })
for rep in ${array[@]}
do
echo $rep
git clone ${branch} git@github.com:${username}/${rep}.git && cd ${rep} && git config user.name "${author}" && git config user.email "${email}" && git checkout --orphan latest_branch && git add -A && git commit -am "commit message" && git branch -D master && git branch -m master && git push -f origin master && cd .. && rm -rf ./${rep}
echo -e "\n"
done
date⚠️记得替换成你的仓库配置!(username:仓库路径名 | author:作者名 | email:用户邮箱)
这里使用的是 ssh 下载方式,你也可以使用 http 下载:
https://github.com/${username}/${rep}.git
如果你的仓库特别多,顺序执行比较慢的话,也可以并行处理每个仓库。代码如下:
Shell
#!/bin/bash
date
reps=$1
branch=
if [ $2 ]; then
branch="-b ${2}"
fi
username=jack
author=Jack
email=Jack@email.com
array=(${reps//,/ })
for rep in ${array[@]}
do
{
echo $rep
git clone ${branch} git@github.com:${username}/${rep}.git && cd ${rep} && git config user.name "${author}" && git config user.email "${email}" && git checkout --orphan latest_branch && git add -A && git commit -am "commit message" && git branch -D master && git branch -m master && git push -f origin master && cd .. && rm -rf ./${rep}
echo -e "\n"
} &
done
wait
date最后,我们赋予脚本执行权限,并执行该脚本:
Shell
# 赋予执行权限
chmod +x ./gitclean.sh
# 假设jack有这些仓库
./gitclean.sh Spring,Log4j,Junit
# 也可以指定分支名
./gitclean.sh Spring,Log4j,Junit master执行结束后,访问Github,查看仓库 commit 记录去吧!
还没有评论,来说点什么。