Term

Git Version Control on the Command Line for iOS Devs

It’s happened to us all: we change something in our code, and suddenly, everything seems to be “broken.” This is when version control is a boon—if you know how to use it. In this tutorial, we’ll learn how to use Git from the command line.

Xcode and other modern IDEs have basic options for Git built into their graphical user interface, but you only get high-level control of your repo (Git repository) through the command line. If you’re doing advanced coding or Git management, it’s important to be comfortable with the command line. If you’ve never used the command line before, you might like to check out my other tutorial on the topic:

  • Mac
    Command Line Basics and Useful Tricks With the Terminal
    Vardhan Agrawal

About Version Control

Before we get started, we should review what exactly Version Control is. A Version Control System is a software solution which can easily save revisions of your code and other data so that you can roll back to previous versions, review changes that have been made, and share updates with collaborators. 

There are many advantages and use cases for version control. For example, by reviewing the changes (“commits“) to your project, you can identify who wrote any particular bit of code and why. You can also roll back any changes that were found to be in error or to break functionality.

The most commonly used version control system today is Git, so that’s the one we’ll be looking at in this post. Be aware that there are other widely used systems, though—for example, SVN and Mercurial. 

Key Terms and Concepts

  • repository or repo—contains all the code for a single project, along with the entire change history of each file.
  • working directory—when you edit your code, you’re making changes to your working directory. If you want to save these changes to the repo, you’ll need to make a commit. If all the changes in the working directory have been committed to the repo, the working directory is clean.
  • commit—a group of changes to source files. Usually, these changes are grouped together so that each commit pertains to a single bug fix or feature.
  • branch—the work on a project can be organized into branches. This lets one developer or group of developers work on one feature, while another developer works on another feature. 
  • merge—brings the changes in two branches together. Often, this can be done automatically by Git, but if there are conflicts, you might have to manually tell Git how to merge the files.

Repository Management Services

When you use version control, you create a repository, or repo, and it is most common to host this repo on a Repository Management Service. For the purposes of this tutorial, we won’t be hosting our repo anywhere, so you can focus on actually using version control. If you want, though, you can read up on these repository management services, and you should be able to apply this knowledge to them if you wish. 

A few examples of these are GitHub, Bitbucket, GitLab, and Coding, and they are widely used by developers all around the world. I, and many others, use GitHub because it hosts a huge number of open-source projects. GitHub repos are public by default, but you can create private repos for a monthly fee.

Getting Started

Creating an Application

To start, you’ll need to create a new application in Xcode. For this app, you can use any template you wish, and if you have a current app which has a Git repository, you can use it as well for this tutorial.

Here’s what the IDE should look like just before you finally create your project (when you need to decide the location to store the project):

Figure 1 Creating an Application

Make sure that the box which says Create Git repository on my Mac is checked, as this will ensure that your project will internally have the repo. Later, if you choose to use a repository management system, you’ll be able to push all of this code, and every commit you’ve ever made will show up.

Opening the Terminal

To get to the command line, you’ll need to open the Terminal. You can do this in one of two ways. You can open Launchpad, and there you can find the Terminal icon in the Other folder on the first page of Launchpad. Or you can hit Command-Space on your keyboard and search for Terminal in Spotlight.

Once you open the terminal, you should see something like the following.

Figure 2 Blank Terminal Window

This is called the “command prompt”—you’ll see the current directory, then your username followed by a $.

All right! You’re now ready to learn about how to use version control on the Terminal.

Terminal Commands Cheat Sheet

Here are some of the basic commands I wrote about in my tutorial about getting started with Terminal. You’ll need to know these in order to use the terminal effectively. 

Help

  • help—as the name suggests, you can type this command into the Terminal to get a list of different commands.
  • man —similar to the previous command, this command tells you exactly what a command does (and gives you full documentation) without you having to search Google for it.

File Management

  • ls—lists all of the contents in the current directory. This command comes in handy if you don’t want to use the Finder to browse files—you can simply list them using this command in your Terminal.
  • cd —this command is used to change directories. If you write cd alone, you will move out of the current directory. After using ls (to see the directories), you can write the name of the directory you want to enter.

Changing Preferences

  • defaults —this command is used to modify default system settings, some of which cannot be changed without using the terminal.
  • caffeinate—as the name suggests, this command is used to prevent your Mac from dimming, turning off, or sleeping. To end this, all you need to do is press Control-C.

Text Editing

  • vim —this is one of my favorites. You can edit text files using the default TextEdit (or any GUI-based editor), but vim is basically a command-line text editor—that is, it works entirely within the terminal.

Networking

  • ping —this command allows you to check the server response time of a specified URL or IP address. This may not be useful for every developer, but it is nice to know.

Admin

  • sudo —a way to override your user’s privileges and become a superuser of the computer. You will be prompted for an admin password when you use it.

Different Git Commands

Now that you’ve seen some basic terminal commands, let’s learn about the different things you can do with Git. I won’t be covering them all, but I will teach you about the main ones which you’ll use in day-to-day development. If you ever need any more information, you can just run git help in your terminal for details, and if that’s not enough, for full documentation, you can run man git to get the manual page.

Here’s what the help pages look like:

Figure 3 Help Page

Creating a Project

  • git clone {remote-repo-link}—if you want to clone a repository from a repository management service, you can use this command along with the URL to get a local copy on your computer.
  • git init—if you’re creating a new repository from an existing folder, you can use this command. It will initialize the current folder to be a new repo. Usually, you would do this when you create a project for the first time.

Committing Changes

  • git status—tells you what files in your working directory have been changed. If you have changed files, it might be time to make a commit!
  • git commit -am "{helpful commit message}"—when you’ve made some changes to your project (for example, when you’ve completed a simple feature or made a bug fix), you should commit your change. Be sure to provide a concise and clear commit message, as this will help other developers understand what you have done and why.
  • git add {filename} or git add --all—if you add a new file to your repo, you’ll have to add it to the repo before you commit it. Use the add command. 

Repository Branches

  • git branch {branch-name?}—with this command, you can either list the current branches or create a new one.
  • git merge {branch-name}—merge the current branch with the branch indicated. This will combine the code in the current branch with the named one.
  • git checkout {branch-name}—switch to the indicated branch. This will simply put the current branch aside and make the other branch active.

Repository Management Systems

  • git push—updates the repository in the repository management system. After you’re done making changes and are sure your code is working well, you can push your code so that other members can see the code and pull it.
  • git pull—updates your local working copy of the repo to reflect the latest updates that have been pushed to the repository management system. It’s a good idea to do this before you make any changes if you’re working on a team.

Those are a few of the main commands that you’ll be using in version control to get started, but before we end this tutorial, let’s take a look at a few of these in depth with the Xcode project that we created earlier.

Example of Using Git With an Xcode Project

Now, let’s take a look at a few examples of how to use command-line Git with the Xcode project that we created earlier in this tutorial. Note that we’ll be using the terminal commands above, so make sure you keep referring to them or memorize them.

Navigating to the Project Directory

Before we begin, you’ll need to navigate to the project directory using the commands that are mentioned above (hint: use the cd and ls commands). Once you are there, run ls and make sure that you have something like this:

Figure 4 File Navigation

Ta-da! Now you’re in your project directory and ready to do anything you need to with your project. Simply follow along as we commit and branch.

Commit

Committing your code is the thing you’ll do most often in programming. As soon as you make a working change, the best practice is to commit it along with detailed comments.

Making a Change

To start off, make a change to the Xcode project. For mine, I’ll just add the following line of dummy code in my viewDidLoad() method:

Getting the Status

After you’ve finished adding (or subtracting) a line or two of code, you’re ready to check the status of your project. To do this, paste the following command (minus the $ prompt) into your Terminal window:

You’ll see something like this:

Figure 6 Modified File

The file you’ve modified is highlighted in red, which tells you it has uncommitted changes.

Adding Files to Staging Area

If you want to only commit certain files, you can do so using a “staging area” where only those files will be committed. To add all the modified files to the “staging area,” all you need to do is run the following line of code:

The -A flag that you see means that all of the files that you have modified will get added (-A is for all, and you can also write git add --all). 

To see that your files are ready to commit, simply run the following again:

You’ll see something like this:

Figure 7 Staging-Area File

See? The same file which was red is now green, which indicates that you’ve successfully prepared it to commit. 

Committing Your Code

Lastly, to finally commit your code, all you’ll need to do is run the following command in your terminal, and between the quotes, add a message.

The -m flag tells the Terminal that you’ll be adding a message to your file, and it’s pretty important to be clear with this message. These messages are crucial for keeping track of the changes to your project.

Now, you’ve made your very first commit! You’re on the right track to making your code easier and more secure.

Branches

The second most common thing you’ll do as a developer is to create, merge, and use branches to sort out your code and isolate features before you roll them out to customers.

Creating a New Branch

By default, you’re in what we call the “master” branch. That is the main branch which, eventually, everything should land up in. Best practice, especially working with a team, is to work on new major features in their own branches, which will be merged back into master when complete.

To practice working with branches, let’s create a new branch. To do this, run the following command:

You can name the branch whatever you’d like. 

To see the new branch, you can type:

When you run that command, you’ll see something like this:

Figure 8 Branches

Notice that you can see two branches: master and my-first-branch (or whatever you named your branch). Additionally, you’ll see that there is an asterisk by the master branch, which indicates that you’re currently in that branch.

Checking Out a Current Branch

If you ever need to switch to another existing branch, you will need to checkout that branch. When you do this, you’re leaving the current branch, and all of its code will remain intact (along with your local changes), but your working directory will be populated with the code from the branch you checked out.

Try it out with the following command:

You should get a confirmation which looks something like this:

Figure 9 Switched Branches

Now, you’re switched to this branch, and it should be a clean slate. To confirm this, run git status to check if there are any modified files. 

Merging Branches

After you’re done making changes, you would normally merge the branch to the master branch. We haven’t yet made any changes, so let’s do that now before we merge the two branches.

Make another change to the Xcode project. For mine, I’ll just add the following line of dummy code in my viewDidLoad() method:

You can make any change you like. Just make sure you know what file and what change you made.

After that’s done, run the following line of code again:

Now, you should see the filename in red, and you will need to commit before merging this change back to your main branch. I trust that you know how to do that, so let’s skip ahead to the next step. Double check that the commit was successful with git status.

By now, you should have committed the code, so let’s get ready to merge the two branches. First, run the following command:

This command switches to the master branch to prepare to merge with the other branch that we created. Finally, to merge, run the following command:

You should get a confirmation which looks like this:

Figure 11 Merging with Master

Now your changes from the feature branch have been merged back into master. If the master branch has changed since the branch was created, Git will try to automatically combine your feature branch changes with master. If it cannot do so automatically, it will prompt you to manually resolve any conflicts.

Now you know how to merge branches, create them, and switch between them using just the Terminal!

Conclusion

As you see, it’s not too difficult to do version control with your project, and the rewards are well worth it. Version control is a core development best practice, and you should be familiar with it if you want to work in a professional context. 

I hope this post has given you the confidence to use version control on a day-to-day basis. If you want to learn more about Git, check out some of our animated instructional videos here on Envato Tuts+.

  • Git
    Git Basics: Branches
    Ed Wassermann
  • Git
    Git Basics: GitHub Pull Requests
    Ed Wassermann
  • Git
    Git Basics: The Three Trees
    Ed Wassermann

Powered by WPeMatico

Leave a Comment

Scroll to Top