In the post about Git, we learned some of the basics of Git. Today we will be looking at the basics of Github. Please follow along by looking at this Code School link. It can teach you the basics of Git and Github very quickly! For additional reference look at the Github Bootcamp as well.
Let’s start by creating a repository. We go (via Terminal) into the folder that we wish to host on Github and then type in the command
git init. This will declare that folder as a valid git repository and set up a hidden “.git” folder. We can check the status of this repository anytime by entering in
git status. This will show us what files we have been added, what files have been deleted, and generally how our current files differ from what has been committed to the repo.
Let us add a very short README file in this folder by using a text editor. Once we have created this file, we can add it to our repo with the command
git add README. This command will add the file to the staging area, before we commit. You can think of staging files as preparing a bunch of ingredients for a cooking dish together. Staging prepares the files so that they can be committed; getting the ingredients prepares us for actually cooking. We can then commit the changes. The changes are how our repo differs from what has been added, deleted, or changed in our current configuration. We have to add a commit message when we are committing as well. This helps when we look back at our commits and try to figure out what each commit meant. The command for committing is
git commit -m "Type Message Here". If you change this README file, and enter in the status command, it will tell us that this file has been modified.
Now that we have a working repo locally, we need to create a repo on Github so that we can link the two. Create an account and a repo by clicking here. All you have to do for now is just choose a name for the repo. Once we have set this up on the web, we can move back to our Terminal. If we want to add our current local code to the online repo we would have to execute a command similar to
git remote add origin firstname.lastname@example.org:sifxtreme/trygit.git. Except you would replace “sifxtreme” with your username and replace “trygit” with whatever you called your repo. After we have linked our local repo with the online repo we can then push the code in our local repo online with the command
git push -u origin master. Please note that the first time you do this, you may get an error about insufficient permission. This can be resolved if you follow the guide for adding an SSH key here. Once you have initially pushed the code, you can usually get away with executing the shorter command
git push. Origin denotes the name of the online branch that we are pushing to. Master denotes the local branch the we are pushing from.
The tutorial goes on talking about how to see differences in between versions of files, updating the files to the latest version, reverting to a previous version, and branching. I’ll leave it up to the reader to go through these exercises. If you want to results of our post where we added a README file to our repo, you can check here. Git is a useful tool for version control. It can also be a great way to showcase your code or give back to the online programming community. The best way to learn anything to try it so try using it when you are working on your next project.