How to add a pre-commit githook script during the build phase in XCode
![Oscar de la Hera Gomez](https://www.delasign.com/CDN/headshots/profile.webp)
![A flower that represents Xcode with the text "Add Pre Commit Githook Script on Build" beneath it.](https://www.delasign.com/CDN/images/How-to-add-a-pre-commit-githook-script-during-the-build-phase-in-XCode.webp)
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"](https://www.delasign.com/CDN/images/Create-Folder_2023-06-16-182858_ozes.webp)
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.](https://www.delasign.com/CDN/images/Add-Pre-Commit-File.webp)
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.](https://www.delasign.com/CDN/images/Complete-Script.webp)
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.](https://www.delasign.com/CDN/images/Add-Script.webp)
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.](https://www.delasign.com/CDN/images/Complete-Run-Script.webp)
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.](https://www.delasign.com/CDN/images/Build_2023-06-16-183436_wcdq.webp)
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.](https://www.delasign.com/CDN/images/Confirm-Test.webp)
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.