Using Git with Terminal

Git is an essential tool in the developers toolkit. Git stores your files like a stream of snapshots, allowing you and other collaborators to make changes to projects and keep everything in sync. This Guide introduces using Git with Terminal. You can click the video link in each section for a video walkthrough or watch the video playlist now.

Basic Terminal Navigation

The Terminal is much like the finder, but less pretty. There are just a few basic commands needed to navigate around in Terminal. Using Terminal, we often refer to folders as directories. Check out this Terminal Cheat Sheet for Mac for a more complete list of commands.

Open up terminal, cmd + space "terminal".

pwd Figure out which directory you are currently in by "Printing the Working Directory".

ls List the directories and files inside the current directory.

ls -a include hidden files in the list of directories and files. This is helpful when trying to find hidden files like .git or .gitignore

cd [folder] Go into the folder. e.g. cd Desktop/Developer

cd This by itself will navigate all the way out to your Home Directory

cd .. Back out to the parent directory of the current directory.

open [file] Same as "double-clicking" a file or folder in finder.

open . Opens the current folder or file in finder or it's default application.

clear When you need a fresh Terminal window

Hint If you can't find the path to a file or folder through terminal, search for it using Finder, then drag the file or folder right into your terminal window! If you are trying to change to that directory, you will need to type cd before dragging in the folder.

Creating a Local Git Repository

Local Git repositories are created and managed locally on your computer.

Xcode will automatically create a local Git repository for your project if you select, "Create Git repository on... My Mac", when you first create your project. You should Always select this when creating a new Xcode project.

If you already have a project, where a Git repository was not created when you made the project, you can create a local Git repository using terminal.

Navigate to your Xcode Project folder in Terminal.

git init

Creating a .gitignore File

When collaborating using Git, you will inevitably run into "merge conflicts". However, you will save yourself from a lot of extraneous conflicts by adding a .gitignore file with the proper content.

Navigate to your Xcode Project folder in Terminal.

Use ls -a to list all files including hidden files to check if a .gitignore has already been created.

SSH Setup

Once you've added your SSH key, the SSH repository URL will look like git@github.com:myusername/reponame.git but for your username and project. You won't need to provide credentials (username/password) for any further git commands.

HTTPS Setup

There is no additional setup required for using HTTPS. However, you'll need to enter your username and password each time you run the git pull or git push commands:

Syncing Local Repository With Your Remote Repository

When you have made changes to your project and used the git add ., git status and git commit -m "Message" commands to update your local Git repository, the next step is to sync with your remote repository. Luckily, keeping in sync can be done with only two additional steps, git push and git pull.

Navigate to your Xcode Project folder in Terminal.

Push changes you have made locally to update the remote repository.

git push

Pull changes that have been added to the remote repository by a collaborator to update your local repository.

git pull

Cloning a Remote Repository

If you are collaborating on a project, or just want access to your project from a different computer, you will need to clone from the remote repository.

Get the url of the remote repository. You can access this by going to the remote repository in GitHub and copying the clone link, bottom right.

In Terminal, navigate to the directory you want to clone the repository in.

Clone the repository...

git clone https://yourRemoteRepositoryUrl

Dealing With Merge Conflicts

Inevitably, there will come a time when you AND a collaborator will make changes to the same file and both try to push to the remote repository. This will result in a Merge Conflict.

In the instance of a Merge Conflict, you will probably get an error in the terminal when attempting to push your local changes.

As, the error message suggests in the "Hint", the next step is to perform a git pull. The pull will likely result in a message similar to the following.

NOTE: The error message confirms we have a Merge Conflict in the Main.storyboard file and tells us that we need to fix the conflict and commit the changes.

Use git status to confirm which file(s) are causing the conflict.

Use open /yourfileHere/ to open up the file in conflict; this will open up the file in Xcode.

NOTE: In this particular example, the conflict is in the Main.storyboard file. We are usually looking at the Interface Builder view of this file, so to reveal the underlying code, right-click on the Main.storyboard file and choose, Open As -> Source Code.

Identify the conflicting lines of code; they will be surrounded by, <<<<<<< HEAD >>>>>>>bunchOfNumbersAndLetters and separated by =======.

Choose one to keep and one to delete. Do your best to figure out what the differences are; in this example, we see that the initialViewController is set to a different ID in each, "BYZ-38-t0r" vs. "NFr-Eg-PaI".

Clean your Project, cmd + shift + K

If your resolved conflict was in the Main.storyboard file, right + click on the storyboard file and choose Open As -> Interface Builder - Storyboard.