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

rjm*_*nro 2 git version-control history code-formatting

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

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

小智 5

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.
Run Code Online (Sandbox Code Playgroud)

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

警告!重写的历史将具有所有对象的不同对象名称,并且不会与原始分支会聚.您将无法在原始分支的顶部轻松推送和分发重写的分支.如果您不知道完整的含义,请不要使用此命令,并且无论如何都要避免使用它,如果简单的单个提交就足以解决您的问题.(有关重写已发布历史记录的详细信息,请参阅git-rebase(1)中的"从上游重新恢复"部分.)