# Format Swift with a Git Commit Hook

DevFeed: [Format Swift with a Git Commit Hook](<https://devfeed.tech/articles/format-swift-with-a-git-commit-hook-21085.md>)

Original publisher: [Read original article](<https://useyourloaf.com/blog/format-swift-with-a-git-commit-hook/>)

Author: Keith Harrison

Published: 2025-11-24T10:29:58Z

Content type: tutorial

Language: en

Sources: [K. Harrison](<https://devfeed.tech/sources/k-harrison.md>)

Topics: [Swift](<https://devfeed.tech/topics/swift.md>), [Git](<https://devfeed.tech/topics/git.md>), [configuration](<https://devfeed.tech/topics/configuration.md>), [Development](<https://devfeed.tech/topics/development.md>), [Code](<https://devfeed.tech/topics/code.md>)

Tags: [auto-layout](<https://devfeed.tech/tags/auto-layout.md>), [bash](<https://devfeed.tech/tags/bash.md>), [configuration](<https://devfeed.tech/tags/configuration.md>), [developer](<https://devfeed.tech/tags/developer.md>), [development](<https://devfeed.tech/tags/development.md>), [git](<https://devfeed.tech/tags/git.md>), [git-commit](<https://devfeed.tech/tags/git-commit.md>), [ios](<https://devfeed.tech/tags/ios.md>), [objective-c](<https://devfeed.tech/tags/objective-c.md>), [swift](<https://devfeed.tech/tags/swift.md>), [wwdc](<https://devfeed.tech/tags/wwdc.md>), [xcode](<https://devfeed.tech/tags/xcode.md>), [xcode-16](<https://devfeed.tech/tags/xcode-16.md>)

## AI overview

This tutorial explains how to format Swift code automatically with swift-format and a Git pre-commit hook. It covers Xcode 16's bundled formatter, .swift-format configuration files, a Bash hook that formats staged Swift files, hook installation, and bypassing the hook when needed.

## Source excerpt

How do you automatically format your Swift code every time you commit it to your Git repository? Swift Format There are several Swift formatting tools. Xcode 16 shipped with swift-format included in the toolchain: $ xcrun --find swift-format /Applications/Xcode.app/Contents/Developer/Toolchains/ XcodeDefault.xctoolchain/usr/bin/swift-format To run the formatter: $ swift format MyFile.swift Note: This runs the swift-format bundled with the active Xcode development directory (beta or release). Configuring swift-format The Xcode settings let you configure various formatting options including indentation but I prefer to use an external configuration file for swift-format. By default, swift-format looks for a configuration file named .swift-format in the same directory as the files you are formatting. If there isn't one it looks in each successive parent directory. If it doesn't find a configuration file it uses a default configuration. You can dump the default configuration to get started: $ swift format dump-configuration > swift-format Modify the configuration file to meet your own code style: { "fileScopedDeclarationPrivacy" : { "accessLevel" : "private" }, "indentConditionalCompilationBlocks" : true, "indentSwitchCaseLabels" : false, "indentation" : { ... I keep my default configuration file in my base Developer directory: $ mv swift-format ~\Developer\.swift-format When needed, I can override the defaults by placing a modified configuration file in the project-specific root directory. Note: Don't forget to prefix the configuration file with a . Git Commit Hooks Git Hooks are a way to run custom scripts when different Git actions occur. A pre-commit hook runs when you commit changes to a repository. Here's the pre-commit script I'm using to run swift-format: #!/bin/bash GIT=/usr/bin/git GREP=/usr/bin/grep SWIFT=/usr/bin/swift # Staged (cached) added/copied/modified/renamed files FILES=$($GIT diff --cached --name-only --diff-filter=ACMR | $GREP '\.swift$') # Exit ear