Search code examples
htmlgitformatting

Avoid auto-formatting source file on git commit


I'm experiencing something strange when I (git) commit an html Angular template. I have this code:

<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"
/>

After I commit this file, my file is automatically formatted like this:

<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"
/>

As you can see, there are some changes (new lines) in my code. No matter if I commit with SourceTree, Tortoise Git or git command line, the file is formatted and if I change manually my file again as before the commit, when I do a new commit the file is formatted again. Something even stranger is that the new commits have no modified files!, the list is empty. I've been researching about some pre-formatting rules or something similar in git but no luck... Any ideas? Thanks


Solution

  • It is probably the case that you have some sort of hook installed which is causing things to be automatically formatted. First, look to see if you have core.hookspath set: git config core.hookspath. If so, your hooks are in that directory; otherwise, they'll be in .git/hooks.

    Look through that directory and look for any pre-commit hooks or other hooks with commit in the name. You can inspect them to see what they're doing, and then, if you don't like their behavior, rename them or delete them.

    If you don't have any hooks installed, then the likelihood is that your editor is automatically invoking a formatter on save. How to disable that functionality would be dependent on your editor, and you should read its documentation to determine how to fix it.