How to use git with Xcode
Optimize your git workflow with these 5 lesser-known tips in Xcode
It’s highly probable that you use git in all your projects.
Using a version control system ensures that our work is well regarded. As an iOS dev, you might be using a tool like Sourcetree, GitHub Desktop, or even just the console to interact with your git system. Although those are some powerful tools, there is one more that you may not be using and has a lot of potential: Xcode itself.
Given that it’s the IDE that we use every day, let’s see how we can improve our git workflow with 5 tips that you may not know.
I'll be using Xcode 15 (Beta 5) and GitHub for this demo
Connect your git account
The first thing we need to do is to connect our git account to Xcode.
- Go to
Xcode > Settings
or just⌘ + ,
- Select
Accounts
and click the plus button - Pick the git provider that you use and follow the instructions.
I’m assuming that your project was already initialized with git. But, in case you didn’t init the project, you can do it in
Integrate > New git repository
git history
We can access the Source Control Navigator by clicking on the second icon of the Navigator Menu. From there, you can access your branches (local & remote), tags, and stashed changes.
- If we click on a specific branch, we can view all the commit history.
- If we double-click on a specific commit, we can easily view the commit’s changes.
git blame+
Get to know who made a specific change in the code. This is very useful if you work in a team and want to get a better understanding of some logic, or even if you work by yourself and want to remember why you did a particular change some time ago.
- Right-click on the line (or chunk of code) you want
- Pick
Show Last Change for Line
- We will see the author and commit’s information (date, message, hash)
- If we click on
Show Commit
, we can see the full change that was made in that specific commit.
git restore
What I like most about this is that we can easily restore chunks of code instead of all the changes in a file. Just click in the blue sticks on the left of the file and select Discard change
git diff
Sometimes we want to check what changes were made in a specific commit. Or maybe we want to compare a file with different commits to see how the file evolved over time.
Click on View -> Show Code Review
(you can also click the arrows icon in the top corner section of a specific file).
If we open a modified file, we will see our changes compared to the last commit highlighted in blue.
In addition, we can check different commits. To change the commits, go to the bottom-left of the file editor and pick the commit you want.
git branches & pull requests
We can quickly create branches from the navigator panel by right-clicking on Branches
.
From there, we can access all the operations we can perform on a specific branch, like switching to it and merging.
To push a branch, we need to go to Integrate -> Push
We can craft a new Pull Request directly from Xcode as well!
Go toIntegrate -> Create Pull Request...
and fill in all the information as you would do it in GitHub.
We can access the basic git operations for the menu:
Integrate
orSource control
depending on your Xcode version.We can: Commit, Stash, Pull, Fetch, Stage & Unstage, Cherry-pick commits
Did you know about these utilities? Do you have any others that you use on your day-to-day? Let me know!