How To Connect an Existing Project to GitHub

Mitchell Wright
Mitchell Wright
Sep 24 2018

This video demonstrates how you can link a project that you've been working on to GitHub. This allows you to store that project off-site with the help of version control. So, I've been doing a lot of work, and I have this project called TableExample. And here I am in the terminal at the command line in my TableExample folder.

Let me print the working directory using the pwd command so you can see that the folder itself that's surrounding this is called TableExample, and inside it, I've got my source code and I've got my Xcode project because this is a code that I am developing on the Mac for iOS. However, what I'm going to show you isn't specific to the Mac or iOS, you can perform these commands for any operating system and any project.

What I'm going to do is connect this project to an off-site version control repository, in this case, I'm going to host it on GitHub. 

As my first step, I have to establish this as a Git repository. Well, that's easy to do. I say git init.

When I say ls, I can see all the files both hidden and not hidden, and anything that is a folder or directory will have a slash after it. So, I do that and I can see that this command created this Git subfolder. Yay. Now, because this is a new Git repository, one of the first things I want to do...not really, the first thing I want to do is I want to do an initial commit. So, I start tracking everything in this folder, git add ., and then I do git status, and as you can see, there is a lot of stuff that needs to be committed.

But everything's in green, there are no issues, there are a lot of tracked items, but I am ready to commit. So, let me do that. I say git commit -m "Initial commit" And again, I get a very long message and it tells me that it added all these files, and now when I do git status, there's nothing to commit. My working tree is clean. And when I do git log, it shows me that I have successfully done my initial commit. I'm now ready to work with GitHub.

So, I've got a repository on my computer and now here I am at GitHub, so I start a project. I want to rename this TableViewExample, and I'm gonna skip the description now, but normally, you would enter a description for your repository. This is a public document. I'm gonna keep this public but down here where there are several options for using a README file, a gitignore file, or a license. I need to skip these. 

If you have an existing repository, skip these items and just go ahead and create the repository. And when I do that, instead of showing me a repository page, it shows me a whole list of setup items. Now, I already have a repository, so I don't need to create a new repository. Here we go, push an existing repository from the command line. And there are two commands here, the first one is a git remote command and it explains how I add a remote GitHub component to my repository. 

And the way you do that is you add origin and this URL. Now, it may seem very weird to call the origin something that's stored off-site. After all, I develop this on my computer. But to anyone who's not me because I'm creating this site, that is going to be the origin of all my published material.

And, because of that, the site at GitHub is the origin, not the material that I created on my computer. So, I'm going to do that and I can do that just with copy and paste. So, here is my terminal, and I just copied that and I paste it here, and let's see if my computer explodes, it won't, there.

Now, I got no feedback because, you know, UNIX git whatever, but I can say git remote -v, and v is short for verbose, and now when I press Return, it tells me that the origin has been set to exactly this, TableViewExample. Oh, I have a different name. Well, I'm going to live with a slightly different name, I just think that it's actually a better name on GitHub than it is on my computer. 

But there are two origins and why is that? And the answer is that the way Git is configured, you have one fetch URL where you pull your information from, and you have one push URL where you push things to. Now, because this is not a forked repository and you do not have much complications, these are just basically the same thing.

And for now, just don't worry about the fact that there are two of them, everything's working the way it should. Now, let me go back to GitHub because remember, they gave me two commands, not one. And, the second command is git push -u origin master. So, this is sort of backwards and wacky.

So, let me explain each of these parts. Master is a branch. I know you probably haven't worked with branches yet but every GitHub repository, and every Git repository which is much more important here, has a branch name even if you are only working on a single line of development, and the main branch is always called master. 

So, if I go back to the terminal and I say git branch, it shows me that there is only one branch for this repository and it is called master. And what's more is there's a small asterisk right next to it showing that I am on the master branch. The master is in green, so it is in good shape. So, what this is saying is push the master. 

Now, where does it send the master? It sends it upstream which is what -u means to the origin, and as you already saw, that origin is at GitHub in this repository. So, let's give this a try, and it worked. Fantastic. Now before I reload this GitHub page, I want to point out that the reason this command is so topsy-turvy is that this is UNIX.

And UNIX, you put the flagged items first followed by the unflagged items. So you're saying git push from master to origin, and that's why origin appears first. It's just the way that UNIX works. After this though, you just need to do git add, git commit, git push. This is really the only time for a single branch project that you need to do this.

But we did our push, if you remember. Here is the terminal, everything has been written up there, bunches of files with plenty of material. And, I'm going to go ahead and reload, so here we go. And look, it is a proper repository now. It has one commit, one branch which is the master branch, and it has my Xcode project, and it has my source code.

So, I've set this all up perfectly. Now, if I want to go into Xcode and make changes, all I have to do is every time I make a significant and self-contained update to this project, I just track the new changes, commit them, and push them here up to GitHub. This video showed you how to take a project you've been working on, place it under version control, and connect it to GitHub so you can share it with other people and reap the benefits of off-site storage.