我正在尝试使用代码格式化工具格式化整个存储库。这样做时,我想保留有关谁提交了哪一行的信息,以便像 git blame
这样的命令仍然显示正确的信息。通过这个,我的意思是它应该向作者显示之前编辑过每一行(在格式化之前)。
有 git filter-branch 命令,它允许您从时间开始对 repo 的每个修订运行命令。
git filter-branch --tree-filter '\
npx prettier --write "src/main/web/app/**/**.{js, jsx}" || \
echo "Error: no JS files found or invalid syntax"' \
-- --all
运行它需要很长时间,我真的不在乎过去。我只想在不更改每一行的所有权的情况下格式化主分支。我怎样才能做到这一点?我尝试在末尾使用 rev-list
和其他过滤器类型,但它仍然不起作用。必须有一种方法来格式化代码库,同时保留每一行的作者信息。
最佳答案
你可以让 git blame
忽略某些提交,这些提交只做大量的重新格式化等:
创建一个文件 .git-blame-ignore-revs
如下:
# Format commit 1 SHA:
1234af5.....
# Format commit 2 SHA:
2e4ac56.....
然后做
git config blame.ignoreRevsFile .git-blame-ignore-revs
,这样您就不必每次使用 git blame
时都使用 --ignore-revs-file
选项。
点赞https://github.com/github/feedback/discussions/5033将该功能添加到 github 的 web blame viewer 中。
关于git - 如何在不修改 git 历史记录的情况下对源代码运行代码格式化程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60535185/