Using Basic Github Commands

Github is used in most jobs as a way to keep track of different versions, or iterations of a project, as well as to simplify the process of collaboration. So it is very fortunate that we are starting to delve into this tool in class.

 

In a nutshell, Github is a web hosting service where you can store your code in repositories. Many are public, so anyone can see and download the source code; they can make changes to it and suggest improvements via a pull request. This is great for open source projects, where everyone has access to the source code and can contribute. Github uses the version control system named Git. Linus Trovalds, the creator of Linux, developed it in 2005. Git maintains a history of changes made to the project, and allows for the existence of different branches of a project.

 

Now, I’ve had my Github account for a while and have been slowly saving changes to a project I’ve worked on off and on. I knew the basic add, commit, push commands, but, as I always worked alone and on one computer, I never had the need to learn commands beyond that. We haven’t yet done a team project in class, but we do have an assigned computer that lives in the classroom, but I also like to play a little on my projects at home. So now I have the situation where I pushed my changes up to Github in class, and I wanted to copy my project to my home computer, make changes and save them on Github, and then update my files on the class computer to continue working. So the first part went well; at home I cloned, or downloaded, my project, improved it a bit, and then pushed the changes back up to Git. Back in class the next day, however, I cloned the project rather than using a pull to just update the changes, ending up with a project folder within my project folder, which I moved to another location. I worked until I was quite satisfied with my project; I had nearly finished it in class. But then, lo and behold, when I tried to push the changes up to Github, I got an error. After a bit of muddling around at the command line, suffice it to say that when I looked at the repository at home, it showed none of the changes that I had made in class! Now I was despondent; had I lost all that work?

 

Well, no. Git has version control, so it would have been possible to track the latest version in its history. This wasn’t necessary, however, as the latest version of the project was still on my classroom computer. Crises averted…. for now. But I knew I had to have a better understanding of how the Git commands function.

 

The inner workings of Git are quite complex and understanding all the commands in depth is a subject much longer than one blog post, and hence I will limit myself here to explaining briefly how to use “git clone”, “git add”, “git commit”, and “git pull” to work on two or more computers.

 

I will assume that you have already created a Github account at github.com, and also created a new repository for your project. I will also assume that you have downloaded Git (https://git-scm.com/downloads ) onto your computer.

 

Once you have Git installed on your computer, open up terminal and navigate to the directory you wish to place your project in. Use the command “mkdir” to create a new directory for your project.

$mkdir projectname

Then navigate into that directory:

$cd projectname

Now you need to initialize this directory as a git repository:

$git init

You can then create your project files. When you are ready to save your work to your online repository you have to add the files first:

$git remote add origin https://github.com/yourGitHubAccountName/projectname

The origin is to link this local account to the remote, online repository and only needs to be done the first time you are saving files.

$git commit -m “first commit”

$git push -u origin master

 

It is useful to understand how git keeps track of the files and commits. In the local directory, git keeps three “trees”. The actual files are stored in the working directory. There is also an index where files are “staged” or indexed for tracking by using the command “git add”. The final tree points the “head” to the last commit made; this is done via the “git commit” command.

Here is a simple visual diagram found on https://rogerdudler.github.io/git-guide/

 

Keep in mind that this all happens locally. In order for the changes to be reflected on your Github account you need to “push” them up to the remote repository.  That happens with “git push” command.

 

So now that you can make changes on your first computer and save them onto Github, and you want to add a second (or more) computer to work on your project. Well as you may have guessed, you need to install Git on the second computer. You then open up a terminal and navigate to the folder where you want your project to be stored. Go to your online Github account and navigate to the project you want to clone, or download. To the right, there is a green button that says “Clone or Download.” If you click on this, you can copy the URL for that repository. Then the command to copy the project is simply:

$git clone urlForYourProject

This will download a whole directory containing your project. You can edit to your delight. When you are finished, you will again add, commit and push.

$git add .

The “.” Adds all the files in the directory to the staging tree. You can also simply specify a filename if you only want to save one file.

$git commit –m “description of the modifications made”

$git push –u

Your work should now be saved onto your Github repository.

 

Later, when you are ready to work on the project on the original computer, you will need to download the changes that you previously made and which are now stored on your Github repository. This is accomplished by using the “pull” command.

$git pull origin master

 

So you can see that it is quite simple to work between two or more computers simply by following these steps. Perhaps in a future blog we can tackle more complex commands.

Leave a Reply

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