CompSci How2: Using Git and GitHub to Manage Your Code

This article is part of a series of brief tutorials I’m writing on performing particular tasks with computers. Indeed, this series may seem random, because I’m writing it partly with selfish intentions: I want to create an easily searched and centrally accessible archive of notes for myself, to inform both my project work and my teaching. I started compiling these tutorials as Word documents on my computer, but that seemed a bit too selfish. So, I will now post these how-to’s on this forum so that others can find them, read them, and use them.

Using Git and GitHub to Manage Your Code

Git is one of the most popular tools for managing the code of projects, particularly projects where multiple people work on the project. Git supports both local and remote storage of code. Remote code storage is facilitated by GitHub. GitHub is a social-media-style website and tool for storing code centrally online.

Git stores code in a repository. As just mentioned, a repository maybe purely local, or it may be linked to a remote storage location hosted on GitHub.

Below are instructions for setting up git and github on your Windows, cloning a project, editing and adding its files, committing and pushing those files, and then pulling down changes.

For Windows installation, download git for windows here: https://git-for-windows.github.io/. Run the resulting executable Git-2.11.0.3-64-bit.exe. Run through the sequence of dialogs as shown here:

 

 

 

For mac or Linux installation, this website provides help installing on these other operating systems:

https://git-scm.com/book/en/v2/Getting-Started-Installing-Git

You can use the command line in git bash to execute git instructions. For example, if you wanted to create a new, locally managed repository of code, you would first move to the folder where you want to store the project’s files, and then you would type git init. You would then create the files that comprise the project, add the files to the “staging area” where they will sit until you commit them. Here are the instructions:

git add nameoffile1 nameoffile2 (or git add -a to add all)

git commit -m “comment about what you added or modified”

But git becomes a lot more powerful when you can share your work with teams online. For that, you need to use github. Here are the steps.

Create an account at https://github.com.

Log in.

Create a new repository by pressing the green New Repository button and then filling out the ensuing web page. You’ll establish the name of the repository at that point.  Once you click “Create Repository”, you’ll see the contents of the repository on the next web page. The url address of the repository set up in the figure would be https://github.com/username/repository_name/.

You then need to sync your local computer. To do that, perform the following commands.

Launch the git tool by typing Git Bash at the Windows Start menu.

If this is the first time you’ve used git, you should configure your username and email address using these commands:

git config –global user.name “username”

git config –global user.email “your@email.address”

Now, move to the folder that you want to hold the repository’s files.

Type git clone url-of-repository. For example, for the project I’ve been working on in this example, I’d type

git clone https://github.com/username/repository_name/

This links the directory on your local machine with the github site’s contents.

Go ahead and edit and add files as you wish.

Type git status to see a list of files you’ve changed or added.

Type git add -A (or git add .) (The period means “all”) to add all the files to the “staging area”, which is a temporary holding place where files sit before being pushed up to the github website.

Type git commit -m “Message” to add commentary about the changes you made.

Type git push to send the files with their accompanying commit message to the github cloud.

The next time you come back to this code, go to the directory where you are hosting the repository locally, and type

git pull

to pull down the latest changed files.

Here is an excellent Youtube video summarizing these commands:
https://www.youtube.com/watch?v=0fKg7e37bQE

 

Here is another excellent tutorial:

http://kbroman.org/github_tutorial/

About Ray Klump

Associate Dean, College of Aviation, Science, and Technology at Lewis University Director, Master of Science in Information Security Lewis University http://online.lewisu.edu/ms-information-security.asp, http://online.lewisu.edu/resource/engineering-technology/articles.asp, http://cs.lewisu.edu. You can find him on Google+.

Leave a Reply

Your email address will not be published. Required fields are marked *