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

Oscar de la Hera Gomez
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.

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.

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.

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.
delasign logo

Book a Free Consultation.

An icon of an email.

Click here to email us.

Fill in the details below to book a free consultation or to let us know about something else. Whatever it is, we are here to help.

How can we help you ?

Contact Details