Link your XCode project with GitHub
In this tutorial I’m going to go through the steps involved in linking your GitHub account with XCode, creating a new iOS project and pushing it to the remote repository. All without leaving Xcode! 😅
1. Prerequisites
Create your global gitconfig file
First, open the terminal and check if you already have a global .gitconfig file using this command:
git config --global --list
- This command will return the location where the global gitconfig file should be, only if it does not exist yet. Otherwise, it will list all the contents of the file.
- If the file does not exist, create a text file .gitconfig in that location using touch *expected_gitconfig_location* .
Add your credentials
git config --global user.name "github_username"
git config --global user.email "github_email"
Create a personal access token
This token will be used instead of your GitHub password when logging into your account. In order to create it, go to GitHub > Settings > Developer Settings > Personal access tokens > Generate new token.
Give it a descriptive name, something like XCode, and select at least the required scopes:
Once your token is generated, save it somewhere safe, since you won’t be able to see it again.
If you feel stuck at any of the steps, you can check out this link for a step by step explanation.
2. Add your GitHub account to XCode
-
Open XCode and go to Preferences
-
Tap the add icon and select GitHub from the list
-
Enter your username and the personal token created previously and sign in.
-
You can see your account has been added to XCode.
The repositories will now be cloned using HTTPS. That’s perfectly fine for demo projects like this, but for large production projects, you might need an extra layer of security. In order to accommodate that, it’s recommended to use a safe SSH connection.
Since SSH protocol it’s a much broader topic and not in the scope of this tutorial, I’ll cover it in another article.
3. Create a new XCode project
-
Go to File > New > Project
-
Choose a template for the project. Here I’m creating an iOS project.
-
Name the project as you wish, with the recommended Pascal Case naming convention.
-
On the next step, make sure to check Create Git repository on my Mac. If this is omitted, you’ll have to create a local repository manually.
4. Create remote GitHub repository
-
Open Source Control navigator, right click on Remotes and then on New Remote.
-
Fill in the repository name, add a simple description, select the project visibility (public or private) and tap on Create.
-
The new repository should appear on your GitHub profile, under Repositories tab.
5. Add your first commit
You can directly commit from XCode, there is no need of using the terminal or other third party apps like SourceTree.
Here I’ve modified the padding value, action being signaled by the blue vertical bar in front of the changed line and the M after the file name. In order to commit:
- Open Source Control and click on Commit.
You can observe other possible git operations from within XCode, like push, pull, discard changes or create pull request.
-
Here you can see all the changes since the last commit. In my case, there is just one line changed. Add a meaningful commit message and press Commit.
-
Also, make sure Push to remote is checked, otherwise your changes will be only commited locally, but not pushed to GitHub.
- If no errors occur, your code should be pushed to GitHub.