避免在 git 提交时自动格式化源文件

Avoid auto-formatting source file on git commit

我 (git) 提交 html Angular 模板时遇到了一些奇怪的事情。 我有这个代码:

                                            <button
                                                [disabled]="votingListService.loadingService.keys['loaderId:' + voting.id]"
                                                (click)="downloadResourceFiles(voting)">
                                                Download
                                            </button>
                                            <img
                                                *ngIf="votingListService.loadingService.keys['loaderId:' + voting.id]"
                                                src="assets/images/loading.gif"
                                            />

提交此文件后,我的文件会自动格式化为:

                                            <button
                                                [disabled]="
                                                    votingListService.loadingService.keys['loaderId:' + voting.id]
                                                "
                                                (click)="downloadResourceFiles(voting)"
                                            >
                                                Download
                                            </button>
                                            <img
                                                *ngIf="
                                                    votingListService.loadingService.keys['loaderId:' + voting.id]
                                                "
                                                src="assets/images/loading.gif"
                                            />

如您所见,我的代码中有一些更改(新行)。无论我是使用 SourceTree、Tortoise Git 还是 git 命令行提交,文件都会被格式化,如果我像提交前一样手动更改我的文件,当我进行新的提交时,文件会被格式化再次。更奇怪的是,新提交没有修改过的文件!,列表是空的。 我一直在研究 git 中的一些预格式化规则或类似的东西,但没有运气...... 有任何想法吗? 谢谢

可能是您安装了某种挂钩导致自动格式化。首先,查看您是否设置了 core.hookspathgit config core.hookspath。如果是这样,你的钩子就在那个目录中;否则,他们将在 .git/hooks.

查看该目录并查找任何 pre-commit 挂钩或名称中带有 commit 的其他挂钩。您可以检查它们以查看它们在做什么,然后,如果您不喜欢它们的行为,请重命名或删除它们。

如果您没有安装任何挂钩,那么您的编辑器可能会在保存时自动调用格式化程序。如何禁用该功能将取决于您的编辑器,您应该阅读其文档以确定如何修复它。