我写了一系列的 git 提交,代码格式很糟糕。
在将它们推送到 github 之前,我想在每次提交时运行 git-clang-format
,以在我的历史记录中获得格式良好的代码。
有没有 rebase
和 git-clang-format
的组合可以完成这个?
最佳答案
这看起来像是 git filter-branch
的工作,它可以重写你想要的提交。由于尚未推送这些提交,因此更改它们的内容(以及因此它们的 SHA1)并不是什么大问题。
其效果类似于 rebase 或cherry-picking 的效果,除了您可以为每个重放的提交运行任何命令。
您可以run a filter-branch
over the last few commits :
参见“Reformatting Your Codebase with git filter-branch
”,作者 Elliot Chance
git filter-branch --tree-filter 'git-clang-format' -- <SHA1>..HEAD
考虑到 git-clang-format
syntax ,您只能将其应用于每次提交中更改的文件。
例如,对于 .cpp
文件:
git filter-branch --tree-filter 'git-clang-format $(\
git diff-index --diff-filter=AM --name-only $GIT_COMMIT |\
grep .cpp)' -- <SHA1>..HEAD
2017 年更新,使用 Git 2.14.x/2.15(2017 年第四季度)您有一个说明:
见 commit 2118805 , commit 6134de6 (2017 年 8 月 14 日)作者:Brandon Williams (mbrandonw
) .
(由 Junio C Hamano -- gitster
-- 在 commit a36f631 中 merge ,2017 年 9 月 25 日)
Makefile:添加样式构建规则
Add the '
style
' build rule which will rungit-clang-format
on the diff between HEAD and the current worktree.
The result is a diff of suggested changes.
.PHONY: style
style:
git clang-format --style file --diff --extensions c,h
https://stackoverflow.com/questions/43974371/