Chapter 16: Local vs. Remote Repositories

In previous chapters, we've set up and used local repositories on our computer, but now we are ready to begin backing up our files online. This is the first step in building an awesome portfolio to show a future employer!


What you will learn

  • Creating a Remote GitHub Repository
  • Adding a Remote Repository to Git in Terminal
  • Committing, Pulling, and Pushing flow for GitHub

Key Terms

  • Remote Repository: an online collection of code that can be managed and modified from a local device.
  • Pulling: a term to describe when you download changes from your remote repository into your local repository.
  • Pushing: a term to describe when you upload changes from your local machine to your remote repository.

At this point in the book, we've used Git to back up our code on our local machine, but as the world has progressively moved online it only makes sense for us to store our code online as well. Not just because the cool kids are doing it, but because it's a great way to build a portfolio of apps and coding projects to show off in a future job interview.

We're going to set up a remote Github repository which will allow us to push code from our repository and onto an online server and pull code from online into our local project.

Setting up a remote Github Repository

First, begin by going to www.github.com and log in to your account. If you haven't yet created an account flip back to Chapter 15 of this section to set up your account.

Once there, click the green button that says New Repository (Figure 1.16.1). Alternatively, you can click on the + button in the upper right near your profile picture and select New repository there.

Figure 1.16.1

Screen Shot 2016-10-18 at 6.46.47 PM.png

On the screen that follows, we will need to enter some information (Figure 1.16.2).

Let's begin by naming our repository something like destroytheworld. Below the Repository name field, enter a description if you'd like. This is optional but helpful in explaining what is inside your repository. Beneath the description field is the option to make this repository public or private.

Github offers unlimited free public repositories, but if you want to store your code on a private repository it costs money. There are other Git repository websites like BitBucket which offers unlimited private repositories, but Github is certainly an authority in this realm and is the best known.

Beneath the public/private repository selection, you can tick a box to initialize your repository with a README.md file. I recommend this because any project you find on Github that is well-established and taken seriously has a thorough and nicely-formatted README.md file explaining their project in detail.


Helpful Hint: If you want to see an amazing example of this, search Google for Alamofire and click on the first result. Scroll down the page that loads and you will see the README.md file for Alamofire which is very well written and looks amazing.


The left-most option at the bottom allows you to add .gitignore. This allows you to declare which files you want GitHub to ignore when you push your local changes up to your remote repository. We won't be using this, but it is useful if you're building an app that includes any type of information you wouldn't want everyone to know – like secret API keys or account information.

The right-most option at the bottom lets you add a license to your project. We won't be doing this either, but it's nice to know that you can.

Figure 1.16.2 Screen Shot 2016-10-18 at 7.02.59 PM.png

After filling out the Repository name field, giving it a description, ticking the Initialize with README.md box, and ensuring that we're making a public repository, click Create repository.

Preparing to connect a Local Repository to a Remote GitHub Repository

We are now at a good place to connect our remote repository on GitHub to our local machine so that any files we create or changes we make can be pushed up and saved online.

Since we've already set up SSH keys to connect our computer to our GitHub account, we can use SSH as a method to transfer files.

Click the green Clone or download button then click Use SSH (Figure 1.16.3).

Figure 1.16.3

Screen Shot 2016-10-18 at 7.22.45 PM.png

Copy the link that is provided and save it for later (Figure 1.16.4). We will use this to connect our remote repository to our local machine.

Figure 1.16.4

Screen Shot 2016-10-18 at 7.26.31 PM.png

Setting up our project and Terminal

Next, open Terminal and Xcode as we will be using both of them.

In Xcode, click Create A New Xcode Project, then Single-View Application, name it destroy-the-world, tick Create Git repository on My Mac, and then save it to the Desktop.

By ticking the "Create Git repository..." box, we essentially run the command git init and initialize an empty local Git repository.

Switch over to Terminal and type cd Desktop/ then cd destroy-the-world to enter our project folder. Type ls and press Enter to see the contents of our project folder (Figure 1.16.5).

Figure 1.16.5 Screen Shot 2016-10-18 at 7.39.08 PM.png

As you can see, there is a folder and an .xcodeproj file with the name destroy-the-world.

Making changes to commit

Click on the folder destroy-the-world on the Desktop and double-click on destroy-the-world.xcodeproj. We're going to make some changes to our project.

Click ViewController.swift and inside of the viewDidLoad() function, add the following:

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    print("Hello, World!")
}

Now we've made a change we can commit. Switch over to Terminal, type git status, and press Enter. You should see some red text saying that you've made changes to ViewController.swift.

If we were to commit these changes right now, it would only commit them to our local machine as we have not yet set up our remote repository on GitHub. Let's do that now!

Connecting a Local Repository to a Remote Repository

In Terminal, we need to add a remote repository. Earlier, you copied a link from GitHub for later use. Now is the time!

Type the following command and then paste the link you copied from GitHub into the Terminal. It should be similar to mine below:

$ git remote add origin git@github.com:kalub92/destroytheworld.git

Press Enter and just like that we've added a remote repository named origin and linked it to the link from our GitHub account. We can now commit and push our code up to GitHub!

Committing files, Pulling in changes, and Pushing to GitHub

Committing Local changes to Git

To push our files to GitHub, we need to go through our standard Git procedure. Enter the commands below followed by Enter in Terminal:

1.git add -A 2.git commit -m "First commit to push to GitHub."

Pulling Remote Changes Into Our Project

Let's pause for a moment and think about what we have going on. We have a local repository containing an Xcode project and we have a remote repository containing a README.md file. We need to pull down the README.md file and merge it into our project.

To do this, type the following:

$ git pull origin master

This command downloads files from our remote repository called origin and pulls it into our master branch (Figure 1.16.6).

Figure 1.16.6 Screen Shot 2016-10-19 at 4.35.30 AM.png

Run ls in Terminal again to check if Git was able to pull down that README.md file (Figure 1.16.7). Looks like it did!

Figure 1.16.7 Screen Shot 2016-10-19 at 4.37.50 AM.png

Pushing Local changes to GitHub

All we need to do to push our local changes up to GitHub is enter the following command:

$ git push origin master

The message that prints out in Terminal explains everything that Git is doing to push our files online, but all you need to know is that the current commit is being pushed. The result should look similarly to this (Figure 1.16.8):

Figure 1.16.8 Screen Shot 2016-10-19 at 4.44.29 AM.png

Verifying that we pushed it real good

Let's go to back to our web browser and find our project. It should be hosted at: github.com/your-username/destroytheworld in case you closed the tab.

Before we pushed changes we simply had a single README.md file. Now refresh the page and see what's changed (Figure 1.16.9):

Figure 1.16.9 Screen Shot 2016-10-19 at 4.47.36 AM.png

Wrapping up

Yay! We have now updated our remote repository and have saved a backup of our code online. We also covered the basic steps of managing both a local and remote repository in Git with GitHub.

The steps we followed were:

  1. Set up remote repository on GitHub and initialized a README.md file
  2. Created a new Xcode project and initialized a local Git repository
  3. Made local changes to our project
  4. Added our remote repository in Terminal by using git remote add origin git@github.com:your-username/destroytheworld.git 5.) Added and committed our local changes by using git add -A and git commit -m "message" 6.) Pulled in changes from our remote repository (i.e. README.md) and merged them into our project by using git pull origin master 7.) Pushed up our changes to GitHub by using git push origin master

Exercise

Create a new folder on your Desktop and copy in the .playground file from the Polymorphism chapter. Open Terminal, 'cd' into the Desktop then the folder you created, and initialize a git repository.

Go to github.com and create a new repository. Connect your remote repository to the one you initialized locally on your Mac by copying and pasting the SSH command found under the Clone or download menu after clicking on Use SSH.

Add the remote repository by using the command git remote add git@github.com/username/your-repository.git.

Add and commit the files in Terminal, then pull in any remote changes from the remote repository. Then push your local changes to the remote repository.

Refresh the Github page to verify you pushed all changes successfully.