Using Git version control with Terminal and Xcode

Jigyasaa Alemu Sood
The SDCoders
Published in
7 min readFeb 7, 2021

Version control is something that most programmers should know, as programming, contrary to the stereotypical depiction in Hollywood movies, is a collaborative effort!

Well, you might ask, what is version control?

Version control, also known as source control, is a good software engineering practice of keeping track of changes to your code. One of the most popular and commonly used version control systems is Git, and it is hosted on Github. It can handle very small to very large scale projects, so, regardless of the complexity of the application, anyone can use it to keep track of their code changes :)

You might think, oh man, I just wanna code, I don’t care or want to know about version control, why do we need it anyway?

Well, there are a number of cases in which Git version control comes in handy and even comes to your rescue:

  1. You are working on your project alone, but your computer ended up crashing and now all your code is GONE (big oof). Command Z ain’t working, and chanting Hail Mary’s won’t either.
    If you were using Git, then your code would be completely safe, on Github where you can just go and download or clone the latest and up to date code on there within a matter of seconds!
  2. You are working in a group and your project is getting too big for you to keep on just zipping it up and emailing it to your teammates upon every damn change you make just so they have updated code when they start coding their part.
    If you had Git, then all you would need to do is commit and push your code and your teammates would get notified that there is a code update and they would just need to pull your code in one command and boom, now everybody has updated project code!
  3. You are working in a group and y’all spent a week fixing all the bugs in your code and one confused teammate ended up making an error and pushing that same buggy code again, and now everyone has buggy code! Oh no, not again! Command Z can only do so much and now’s not the time to pull all your hair out and take another week to fix your code…

If you were using Git, you could just revert back to a previous version of your code and boom, it’s like that buggy code never happened!

And the list goes on…but you get the point, I’m sure! Having version control is super helpful in keeping track of all your code, bugs resolved, features completed, etc. Through version control you have the ability to view not only your latest saved code, but code you and your team had saved hours, days, months, and years ago!

Before we dive into setting up version control, let me explain some common terms and commands used in version control so you understand what everything means!

  • Repo, short for Repository: it’s a storage space where you project or code resides in, it’s like a folder for your code! There are two types of repos, local and remote.
  • Local Repo: it is going to be on the machine (laptop or computer) where your code is stored at. Locally storing it means only you have access to the code and if your computer crashes or you lose that code, then it would be pretty hard to get it back.
  • Remote Repo: it is going to be hosted on a website, like Github, for example, and your code here is accessible to not just you, but your whole team! If you lose your code on your computer, your code is still safe on here! This is akin to iCloud storage.
  • Committing code: saving code locally on your computer, to your local repo.
  • Pushing code: saving your committed code onto some hosted platform like Github, to your remote repo.
  • Pulling code: downloading updated code from your remote repo to your local repo (machine).
  • Branch: it’s a like folder inside your repo, where you can organize what features are in which folder. Say, you and your teammate are working on an app together and you are working on feature 1 and they are working on feature 2. You can just have two different branches, feature1 and feature2 to keep your code more organized and prevent bugs!
  • Merge: when you use multiple branches, sometimes you want to combine the code in both branches, that is called a merge.

So now that you are familiar with the key terminology related to Git, without further ado, let’s discuss how to set git version control for your project, both via terminal and through Xcode!

** You can use Git version control in the terminal or through Xcode, I will show you how to use it via both methods, you can choose which one you like!

Git version control set up via terminal

  • Step 0: Create an Xcode project. (either a new project or you can even work with an old project that doesn’t have version control yet!)
  • Step 1: go to github.com and sign in and create a new repository.

and then, add a unique name for your new repo:

  • Step 2: Go to your project folder in your terminal.
  • Step 3: on your new repository on Github, you should see a screen as shown the image below which will have a bunch of commands for you to run in your terminal in order to connect that remote repo to your local repo.

Make sure you have selected HTTPS and run the commands I highlighted, in your project directory in the terminal like so:

Now, when you go back to Github, and refresh the page, you should see an initial commit on there! Each commit is a labelled copy of your current code! It represents what has changed since the last version of your code.

Done! That’s it, your project is now connected to your remote Github repository.

Git version control set up from within Xcode

The difference via Xcode is that you don’t need to go to Github and manually create a new repo, Xcode will create a new repo for you automatically!

  • Step 0: Create an Xcode project (either a new project or you can even work with an old project that doesn’t have version control yet!)
  • Step 1: click on the source control navigator to connect to a repo.
  • Step 2: click on the settings at the bottom and add a new repo and follow the steps in the screenshots below!

If it’s saying authentication failed, then you can always add a different account. This is most likely to happen now since Github just enforced 2-factor authentication and you would need to save the token you generate on Github as your credential to log in here.

how to create a Github access token

paste the access token you generated here:

And you’re done! Now, in order to view your repo, you can just click on settings again and view your repo on Github!

and here is what it your remote repo looks like on Github!

How to Commit, Push, and Pull code in Xcode

aight so now you have your repo all set up! Let’s see how to actually use version control in Xcode.

  • Step 1: commit edited code. **never pull anything unless you have already committed (saved) your code

You can either just locally commit to keep track of your changes and save your work locally or commit and push to the remote repo so that your teammates can download your version of the code!

so, commit locally :

or, commit and push your changes:

In order to pull your teammate’s code, just go back to the source control tab on the navigation bar and then click pull (make sure you have committed your local changes first, else they will be overwritten!!)

That’s it! May the Gods of version control be with you :D

--

--

Jigyasaa Alemu Sood
The SDCoders

Software Engineer | App Developer | Entrepreneur | TA @ CodePath