How to add a pre-commit githook script during the build phase in XCode

A step by step guide on adding a pre-commit script to a git repo as part of the build phase to guarantee consistency across your team.

Oscar de la Hera Gomez
Written by Oscar de la Hera Gomez
First published on 06/16/2023 at 12:25
Last Updated on 09/16/2023 at 17:19
A flower that represents Xcode with the text "Add Pre Commit Githook Script on Build" beneath it.

A step by step guide on adding a pre-commit script to a git repo as part of the build phase to guarantee consistency across your team.

SubscribeDownload Open Source Swift Project

The following tutorial demonstrates how to create a pre-commit githook script that first makes sure that a team member is committing to a valid branch and then runs SwiftLint to lint the project. It keeps tracks of the changes created by SwiftLint and commits the changes as part of the process.

Step One: Create the Folder

In Terminal, right click and click "New Folder". Call it ".scripts"

At the root of your git repository, create a new folder called .scripts.

Step Two: Create the pre-commit script

A screenshot of Finder showing the pre-commit file within the .scripts folder.

In the .scripts folder, create a new file called pre-commit.

Step Three: Complete the script

A screenshot of TextEdit showing the script that's found below.

Open the script using a text editor and add the relevant code.

We have provided the pre-commit script available in our Open Source Swift project below.

Step Four: Add a Run Script

A screenshot of Xcode showing the project Build Phases screen. Highlighted is the + Button. If you click it a menu will an appear. We have highlighted the "New Run Script Phase" menu option. Click it.

In the project in XCode, under Build Phases add a new Run Script.

Step Five: Complete the Run Script

A screenshot of Xcode showing the completed run script. Code is available below.

Copy the code below into the Run Script.

cp ./.scripts/pre-commit ./.git/hooks

Step Six: Build

A screenshot of Finder showing the pre-commit script in the git hooks folder.

Build your project and confirm that the pre-commit script appears in the .git/hooks folder.

Are you getting a Sandbox error when building the project ?

To resolve this - in the project's s settings, select the relevant target and under "Build Settings", set "User Script Sandboxing" to No.

How to resolve an XCode sandbox build error

Step Seven: Test

A screenshot showing a failed attempt due to the branch being the main branch. This is expected behavior.

Attempt a commit to confirm that the changes went through.

Frequently Asked Questions (FAQ)

Why am I getting a Sandbox build error ?

The cp command is not permitted in a build script if "User Script Sandboxing" is turned on.

To resolve this - In the project's s settings, select the relevant target and under "Build Settings", set "User Script Sandboxing" to No.

How to resolve an XCode sandbox build error

Any Questions?

We are actively looking for feedback on how to improve this resource. Please send us a note to inquiries@delasign.com with any thoughts or feedback you may have.
SubscribeContact UsVisit our BlogView our ServicesView our Work

Partner with us

We would love to get to know you and see how we can help your organization with its goals and needs.
Let's Talk

Stay Informed

Get occasional updates about our company, research, and product launches.
Subscribe