How I Learnt to use Git and GitHub

This is a simple demo for any aspiring software developer who is new to Git and GitHub. Publishing your code on an online repository is an important skill required for software developers since we usually work through Team work and Collaboration.

You will need a working Internet connection, a github account and a git installation on your computer. You can follow this link from the Official Ubuntu Documentation on how to install and configure git. But it’s only cool for a beginner as far as installation is concerned, then it gets a bit complex for someone who needs a quick guide.

Go to GitHub to sign up for a github account if you don’t have one already.

Here are the steps I used to manage a simple repository on github.

Create a new repository
Login to your github account and click the + symbol at the top-right corner of the page next to your profile picture. A drop-down list will be displayed. Select new repository from the drop-down. A new page will open where you can enter the repository details as shown below.

I highly encourage Initializing the repo with a README since we will clone it to our computer soon.

Getting ready to clone
We created a repository named How-I-Learnt-Git. So, open the repository in the browser then click the Clone or Download button at the top-right corner of the repo as shown below. In the Clone with HTTPS drop-down, click once to select the url and copy it to clipboard.

Cloning the Repository
On your computer, use the git clone command followed by the url you copied from the online repository as shown in the figure below.

Press [ENTER] on the keyboard to start cloning. When done, you should see a message similar to this.

Change the directory to the local repository you just created. To do this, you can use the cd command as shown below.

To see the contents of your new local directory use the ll command. Here is the output of the ll command.

Congratulations! You have successfully cloned a GitHub repository onto your local computer. Next we will see what happens when we make changes to our files and directories, and how we can handle them.

Create a new demo file
Let’s create a new demo file. Open File Explorer and browse to the local repo so we can do this in GUI instead of an all command-line approach. Create a file inside there called new-demo-file.py.

Add sample content to this file as shown in the figure below.

Save the file. Go back to terminal and use the ll command again to view the contents of the local repo. The ll command should now display an output similar to this.

Introducing git status
The git status command is used to show the status of files in our working directory. For example, since we cloned the repository we have made some changes to our working directory by adding a new file called new-demo-file.py. We have also added a few lines of code to this file, therefore the git status command should be able to reveal to us more of what’s been happening. Here is the output from git status.

The important message here is nothing added to commit but untracked files present. And it also suggests we use git add to track them.

Using git add
We use git add to initially add a new file to the staging area. We also use git add to add to the staging area modifications made to an already committed file as well. git add takes an argument (names of files to be added) but in our case we will add everything that is “dirty” using git add * command as shown below.

Run the git status command again to notice how the status of our working directory has changed.

The important message here is that we now have changes to be committed. Also notice the difference in the colour of the new file: new-demo-file.py.

Committing changes
To commit the changes you have made to your local repository, use the git commit command. This way, you ensure that the changes made to your local repository are recorded. So let’s commit our changes, here is how.

We have added -m to indicate that we are going to add a comment. In the above example the comment was “Added new-demo-file.py”. And the output of the operation is as shown below.

When you git status. You will get an output similar to this.

The important message here is Your branch is ahead of ‘origin/master’ by 1 commit. We are therefore ready to push our changes to the online repository on github.

Publishing your work
You publish the commits from your local repositories using git push. git push will send your changes to the remote repository. Here is how to use git push.

Publishing to the remote server on github will require that you sign in with your correct username and password for the github account under which the repository was created. Once this is done successfully, you will receive an output similar to this.

The important message here is Your branch is up-to-date with origin/master. nothing to commit, working directory clean.

From the browser, now refresh the repository and you should be able to confirm that the new-demo-file.py now exists on the online repository too.

You can even open the file online and view its contents. Like this

Congratulations once again! It’s all a matter of learning. You can do more if you properly learn.

All the Best,

Kevin Oyowe