The Swift Blog: A crash course on Git in Terminal
During the last couple of weeks we had some trouble with student’s project submissions, so I thought I might just write a small post about how to use Terminal and upload your files effectively using my beloved Terminal. And without further ado, hear we go.
Here’s what you need to do to get started:
- Head over to GitHub and create a new account if you don’t have one yet, otherwise log in to your account.
- After logging in you will presented with something that might look a bit like this: Click on the “New” button in the top left-hand corner to create a new repository.
- Enter the name of your shiny new project in the text box at the top. For your first project this can be SoccerCoordinator or something. You don’t need to add a description or a README file, but you can if you wish to. Click “Create repository”
- After doing so you will be greet What I want you to do now is click the “copy link” button next to the URL to your repository.
- Now on to the interesting part. Open up your Terminal by either selecting it from the Applications folder or firing up Spotlight by first pressing
Command-Spacebaron your keyboard and then typing in the name Terminal. When Spotlight has found the app either click it or hit the Enter key.
- You will be greeted with an empty Terminal window, your window to the wonderful world of Command Line tools.
- Now here are a couple of things to consider. You can navigate your entire file system with just one simple command, the
cdcommand or “change directory”. For example, let’s say you want to navigate to your Desktop, what you’d need to do is enter
cd ~/Desktop. The tilde
~is your root folder, in other words the folder all your other folders like “Applications”, “Documents” and so on live in. Just as an example of how I manage my files, I have all Treehouse projects inside a folder called “Treehouse Projects”, which is in a folder called “Studies”, which in turn is in a folder called “Programming”. In the Finder this looks something like this: To navigate to this folder I would enter
cd ~/Programming/Studies/Treehouse\ Projects/, then hit the “Enter” key, like so: The
\is an escape symbol which you need to use in order to add whitespace to the path. As said, this is just an example and it’s totally fine to save your files somewhere else, the important part is that you know how to navigate to that location inside Terminal.
- Now, create a directory where you want to save all your project files - it’s ok to do so in Finder if you’re more comfortable with that - and then navigate to that folder in Terminal.
- Remember the URL I asked you to copy to the pasteboard? If not scroll up to point 4 to do so. Now enter
git cloneand paste the URL from point 4 by pressing
Command-V. For me this would look like so:
git clone https://github.com/thedan84/SoccerCoordinator.gitThis will clone your empty repository to the path you defined. Your output should look something like that:
Dan's MacBook Pro:Desktop TheDan$ git clone https://github.com/thedan84/SoccerCoordinator.git Cloning into 'SoccerCoordinator'... warning: You appear to have cloned an empty repository.
Don’t mind that warning, it just means that the folder is empty, but we’re going to change that next.
- Open up Xcode, ideally you will be greeted with a Welcome screen like this:
If not, please press
Shift-Command-1on your keyboard. If you like, you can check the box in the lower left-hand corner to get to this screen every time you open Xcode.
- Click on “Get started with a playground”. You will be presented with a screen like this: Select the “Blank” template and click “Next”. In the following screen enter the name of your project and select the cloned repository folder, then click “Create”.
- Alright, you’re almost good to go. To test out that everything works out, we’re going to upload this empty playground to GitHub.
- Open up the Terminal and navigate to the cloned repository folder.
- Next, enter
git add .. There is a whitespace and a dot
addwhich is very important. Then hit “Enter”.
- Next, enter
git commit -m "Initial commit". Everything inside the quote marks
""is your commit message. Please make sure to add something meaningful here later on. For the first commit “Initial commit” is sufficient though. As always, hit “Enter”.
- Lastly, enter
git push -u origin master. This will upload your local copy to GitHub alongside with all files necessary to run your project. For the first time, you might have to enter your GitHub credentials. After doing so you should get an output like this:
Dan's MacBook Pro:SoccerCoordinator TheDan$ git push -u origin master Enumerating objects: 6, done. Counting objects: 100% (6/6), done. Delta compression using up to 4 threads Compressing objects: 100% (5/5), done. Writing objects: 100% (6/6), 655 bytes | 327.00 KiB/s, done. Total 6 (delta 0), reused 0 (delta 0) remote: remote: Create a pull request for 'master' on GitHub by visiting: remote: https://github.com/thedan84/SoccerCoordinator/pull/new/master remote: To https://github.com/thedan84/SoccerCoordinator.git * [new branch] master -> master Branch 'master' set up to track remote branch 'master' from 'origin'.
Congratulations! You pushed your first project to GitHub!
So, now what, you might ask. How do I get any changes up to GitHub? Fortunately it’s actually quite straightforward.
The only things you really need to remember is how to get to your project file in Terminal (
cd) and then the last three commands I showed you, so
git add . git commit -m “Your commit message" git push
Yes, after the first push, you can omit the
-u origin master. Git is completely set up and knows where to push all changes.
I hope I could help a little with this short introduction of Terminal and git, and that you will use your newfound knowledge to get the most out of it. If you want to learn more about all this, there is a great Git workshop available on Treehouse. Furthermore here is a great, free resource for learning all about the Command Line, Text Editors and Git. As always, if you need any help don’t hesitate to contact me via Twitter.