gpt4 book ai didi

git - 重建 git 历史的脚本,对每个版本应用代码清理

转载 作者:太空狗 更新时间:2023-10-29 14:34:52 26 4
gpt4 key购买 nike

有没有人有一个 git 脚本可以查看历史记录,检查每个版本,应用清理脚本,然后将清理后的版本检查到另一个存储库中?

我有一些我一直在开发的代码,但我没有与代码格式保持一致,例如制表符与空格等。我想重写我的整个历史以符合新标准。

最佳答案

git filter-branch 命令可以满足您的需求。

例如:

# Make a backup!
cp -r <repo> <repo>.backup
cd <repo>
# Replace tabs with two spaces in all .cpp and .h files in all branches.
git filter-branch --tree-filter \
"find \( -iname '*.cpp' -o -iname '*.h' \) \
-exec sed -i -re 's/\t/ /g' {} \;" -- --all
# Delete branch backups created by 'git filter-branch'.
# From the end of `man git filter-branch`; more cleanup
# suggestions there.
git for-each-ref --format="%(refname)" refs/original/ | \
xargs -n 1 git update-ref -d
# You still have ../<repo>.backup, in case something went wrong.

但要小心……这会转换 git 存储库。如果有人有克隆...它将不再连接到您的新仓库。来自 man git filter-branch:

WARNING! The rewritten history will have different object names for all the objects and will not converge with the original branch. You will not be able to easily push and distribute the rewritten branch on top of the original branch. Please do not use this command if you do not know the full implications, and avoid using it anyway, if a simple single commit would suffice to fix your problem. (See the "RECOVERING FROM UPSTREAM REBASE" section in git-rebase(1) for further information about rewriting published history.)

关于git - 重建 git 历史的脚本,对每个版本应用代码清理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2500444/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com