automated code formatting git

I’m working on a project(PHP) and on every commit there are some breaks on code convention. I’m using git for version control. Is there a way for automated code formatting so that all the code stays clean?

  • What is a good way to manage cleanup commits in git for a legacy project?
  • Code formatting before commit to GIT
  • Is there a text editor that will “pretty print” for display without changing the underlying text?
  • Merge strategy for whitespace only changes?
  • Committing when changing source formatting?
  • GIT: restore formatting from master branch but keep non-formatting changes
  • git user info questions
  • Git strategy to keep on applying my changes to someone else's updeting branch
  • Print branch description
  • Git submodules are being -dirty?
  • Git ignoring podfile when gitignore doesn't mention it
  • Keeping API keys and access details for the database secure
  • 5 Solutions collect form web for “automated code formatting git”

    There are two parts to the question: automatically formatting the code, and detecting when it doesn’t conform to your coding standards.

    Automatically formatting the code is not really something you want to put inbetween you and your repo directly. Modifying files, or attempting to modify files, in a pre-commit hook is likely to make a mess. As such it doesn’t matter what vcs you are using.

    Using a tool to format the code via your editor or as a process you run (manually, or semi-automated) as part of your development workflow would be appropriate. For example, vim has the = function to auto-indent code, and as mentioned by others Pear’s beautifier is one possibility to do this.

    Detecting code standard devitions requires a cli tool that tells you when a file does not conform to coding standards – the obvious choice is PHP Code Sniffer (phpcs) – though it could simply be the same tool you use to beautify your code manually (if you use one) and checking that it doesn’t change the file contents.

    You may need to write your own standard to use with phpcs if none of the existing standards match your style.

    You can use a pre-commit hook to trigger a check on the code right before you commit it – If there are code errors found, you’ll be notified about them and the commit aborted. You can bypass your pre-commit hooks using git commit --no-verify

    You may find this repo useful:


    $ more foo.php 
        function bar() {
    $ git add foo.php
    $ git commit -v
    running php/lint.php ...    OK
    running php/phpcs.php ...   FAIL
    phpcs -n -s --extensions=php,ctp --encoding=UTF-8 --standard=Cake '/tmp/cakephp-git-hooks'
    FILE: foo.php
     2 | ERROR | Space indented: Tabs for indents, spaces for alignment (Cake.WhiteSpace.ForceTabIndent)
     2 | ERROR | Line indented incorrectly; expected 0 spaces, found 4 (Cake.WhiteSpace.ScopeIndent.Incorrect)
     3 | ERROR | Space indented: Tabs for indents, spaces for alignment (Cake.WhiteSpace.ForceTabIndent)
    Time: 0 seconds, Memory: 3.75Mb

    (commit aborted, code does not meet code standards)

    $ git commit -v --no-verify -m "dummy commit"
    running misc/happy-commits ...  OK
    [2.1 2c432f1] dummy commit
     1 files changed, 3 insertions(+), 0 deletions(-)
     create mode 100644 foo.php

    (commit succeeded – even though code standards were not met)

    Git knows hooks. You could leverage them to have some form of checkstyle application running before each commit is accepted into the repository. Check the files in .git/hooks/ within your repository. It’s possible to refuse a commit this way. I’m not sure if you can modify the commit though.

    There are a few ‘PHP beautifier’ scripts around (one of them in PEAR), that you could call from git’s commit hook.

    You can use PHP_Beautifier to beautify your php script.

    But I think beautifying in every commit might end up lots of modified line. This will make tough to understand the output of diff command.

    Git has commit hooks which allow you to execute command before your commit. It is not recommended to use these commit hooks to change the source code as this may cause unwanted results.

    You can however make use of a git pre-commit hook to execute PHP Code sniffer. PHP code sniffer is a tool, install-able via PEAR, which will indicate where you wondered off standard. You can setup your own standards or make use of existing standards like the PEAR standard. The commit hook can be set up to display all violations (non standard code) and halt the commit. If code is according to standards, it can allow the commit.

    Here is the link to the PHP Code sniffer:

    Here is nice example of a git pre-commit hook for php code sniffer:

    Some documentation of git commit hooks:

    Git Baby is a git and github fan, let's start git clone.