-
Notifications
You must be signed in to change notification settings - Fork 1
Beginner Project: Github
While Git is great on its own, its real power becomes apparent when you learn how to use it to store your code on a server and collaborate on projects through it. This project will show you how to host a Git repository on Github, and explain the mechanics of pushing and pulling to it. Specifically, it will cover:
Make sure you've done the following before following this project
Server
- "Server" is probably a term that you're familiar with, but you may not have ever interacted with one directly before. In our case, Github is going to act as our Git server, although it's easy (and useful) to set one up yourself. For this tutorial, if I refer to the "server", it really just means Github.
The first thing that we need to do is tell Github to create a place to store your project. Once you log into Github (which, if you're reading this, you've likely already done), you'll be presented with a homepage that looks something like this.
Find the + New Repository
button and press it. You'll be taken to a page that looks like the one below. Fill in a name for your repository and a description, making sure to leave the repo public and to choose not to initialize it with a Readme (we're going to do this step together in a bit).
Once you've hit the Create Repository
button, you'll be taken to the homepage for your new repo and are all set to push your code to it!
Now that we've given your project a home on Github's server, we need to tell your project about it. Right now, the homepage for your project probably looks something like this:
Select and copy the address found at the top of the page (look in the red box in the screenshot). This is the URL for your Github remote repository, which is called the origin
. The term origin
is used to refer to the main remote location for your project, even if it's not on Github. You can as many remotes to a Git project as you want, but that's a topic for another time.
Once you have the URL copied, get your project open in the command line again and run the following commands:
git remote add origin {copied-url}
git push --set-upstream origin master
This will add the new remote address, and then push to it, as well as set some default values while it's at it. Now, go back and visit your page on Github, and you'll see the project we've been working on!
To explain a bit about what just happened: in essence, the code on the server (Github) and the code on your local computer are the same. Git is tracking the exact same set of changes on both computers, and because of this, we can "trade" commits back and forth. We had some commits locally that the server doesn't have, so we'll push
them up to the server. In the next step, we'll pull
some commits from the server down to the local computer. With the flow of pushing and pulling, your code remains updated in both places.
Now that we've added code to the server, we want to learn how to pull code from there. When you're working on a project with multiple people, it's pretty likely that they will add code that you need to retrieve from them. To do this, they would make some commits and push them. Now that the server has their commits, you can pull
the changes from the server so that they get integrated into your code. You and your partner never exchange commits directly -- everything flows through the server.
So mock having a second person working on the project, we'll use the Github website to add a Readme to the project. A Readme is a file that explains anything important about the project -- what it is, what is does, who wrote it and when, who to contact about problems, installation steps, etc etc. On Github, the Readme file is parsed in Markdown and displayed as part of your repository, allowing you to host code and information all together on one page.
On the homepage of your project, find the button that says Add a README
and optionally edit the file that it creates. When you're done, hit the green Commit new file
button at the bottom of your page. Now that your project has a Readme, you'll see its contents displayed at the bottom of the project's page on Github, and a new commit has been generated.
Back on the command line, run the command
git pull
This will contact Github and pull down any new changes that you don't have locally yet. Once it is done, you should see the Readme.md file on your computer.
That's it! You now have a basic understanding of the main principles of Git and how to collaborate with Github.
- Beginner Level