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

标签 git version-control history code-formatting

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

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

最佳答案

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/

相关文章:

Git - 更改分支提示以包含来自另一个分支的提交

GIT 与 assembla 存储库类型个人或公共(public)?

eclipse egit : How to synchronize with remote repo?

version-control - 是否有任何svn责任等同于clearcase?

javascript - 我可以阻止 history.popstate 在初始页面加载时触发吗?

没有提交的 git 分支

git - 我怎样才能正确地 git pull 和 git checkout?

svn - 如何防止 Subversion 合并二进制文件?

php - PHP token 名称 T_PAAMAYIM_NEKUDOTAYIM 的含义是什么?

windows - 为什么 "Program Files"是两个字?