How to work with Git and GitHub

Tony Staunton
Python Code Academy
11 min readFeb 8, 2022

At the moment, how you do you save your code so that it’s easily available to you in the future? Do you backup your code? Can you easily share your code with other developers? Do you have a file called ‘v1’, ‘v2’, ‘v3’, within folders, within folders? Is your finished code file called ‘Final’, ‘Final, Final’, ‘Latest Final’? This is a hugely stressful way to work. One where you’re constantly worrying about how you will save your next piece of code, and find yourself saying ‘I’ll clean up my system for the next project’. This way of working makes it difficult to track your code, file changes, and to retrace your steps should you need to.

When you find yourself unable to answer the questions above, or getting frustrated by the thoughts of your code file structure then it’s time to consider adopting a Version Control System (VCS). In a nutshell, a VCS is a piece of software responsible for managing changes to a file. In this article, we’ll discuss how to use Git and GitHub as our version control solution, and how to use them for collaboration when working as part of a team. We’ll also learn how to install Git, create a GitHub repository or ‘repo’, and edit source code locally so that you can then push it to your GitHub repo. I would recommend that you read this article all the way through first, and then follow along with steps below.

Let’s start by understanding the basics of a Version Control System (VCS).

What is a Version Control System?

A Version Control System, (VCS), is a software tool used for tracking changes to your files and folders. For our puposes, it’s used for tracking code files during development. A VCS allows you to track changes that you have made to a file, and return to a previous version if you need to. This is a great way of monitoring the progress of a project, especially when working in a team. It’s also a great way of having a previous version of your code at hand for times when you completely destroy your current working code, and you will!

Types of Version Control Systems

There are three types of Version Control Systems:

Local version control system — In a local version control system, the version control happens on your local machine, as in, the computer you are currently working on. So if your local machine breaks down, you’ve lost your code file. Also if something goes wrong with a code file you have under version control, if it gets corrupted somehow, any versions created after that initial version will be lost. This happens because the version database is created using the previous file. Finally, a local version control system does not allow you to collaborate with other developers. Essentially your VCS is offline so it is impossible for other team members to access it should you wish them to.

Centralized version control system — In a centralized version control system, members of you team can collaborate on a code file because the file repository is stored on a central server. Team members can access copies of the code file on their local machine and make any necessary updates. The downside of this solution is that if the central server crashes, the code file may be lost. This is because the main file repository and history is stored on the central server and not the individual’s local machine.

Distributed version control system — A distributed version control system operates in much the same way as a centralized version control system, but with some important differences. As a distributed version control system is, distributed, as in there is more than one server that stores the file repository. In other words, if one server crashes, there is another server to operate as a backup. Also, every collaborator working on a code file has access to its version history, and can view a history of its past edits. A distributed version control system ensures that every user has access to a copy of the entire file repository. The level of redundancy offered, makes a distributed version control system, a great solution for developers to store their code, track changes, and share it with other developers.

Now you understand the basics of what a VCS is, and the different types, let’s take a look at two of the most popular, Git and GitHub.

Git

Git is a command line version control tool. If you are new to the command line check out a previous article of explaing the basics. It was developed in 2005 by Linus Torvalds, the creator of Linux. Git works by tracking changes to a particular file, in our case, a code file. Git has become widely used by developers of all programming languages since it gives them the ability to work simultaneously on smaller chunks of a bigger project. Also, should the requirements of a project change, developers can easily revert back to a previous version of their code.

When working with Git, project files or source code are stored in a repository, often called a ‘repo’. The repo can either be hosted online or on a local server.

As discussed earlier, hosting your files on an online server is the safest way to work. There are several online hosting services where you can host your repositories. Some of the most populr include BitBucket, GitLab, and GitHub. In this article, we’ll discuss GitHub.

GitHub

GitHub is an online development platform that you can use to host and maintain your code repositories. There are several advantages to using GitHub, including:

  • Track changes on a file using git
  • Merge code from different file versions
  • Revert back to an older version of your code
  • Test the effect of a new code change without breaking the original code file
  • Easily share code files, assign tasks, and collaborate with other developers
  • Acts as an online backup for your code projects

It is important to note that GitHub as well as GitLab and BitBucket use Git for their version control operations. In other words, you need Git to work with GitHub.

Let’s install Git.

Installing Git on your Machine

On MacOS

The easiest way to install Git on your Mac is to use the Xcode command line tool. Open up your terminal and run the following command:

git –version

If you have Git installed on your machine, the prompt will return the version you have. If Git is not installed you will be prompted to install it.

On Windows

To install Git on Windows download it from Git’s original website. Go to the download page here, click on the appropriate system build and download the Git for Windows exe. Once downloaded, run the file and click Next until the installation is complete.

On Linux

If you are using Ubuntu, you can use apt using the command below:

sudo apt install git-all

If you are on Fedora, you can use dnf with the command below:

sudo dnf install git-all

Creating a GitHub account

If you have a Github account, then you can skip this section. If you don’t have an account, go to github.com and create one now. Click on Sign up, enter your details, and confirm your email address.

Next, we need to connect the Git software installed on your local machine to your GitHub account. This will allow you to move or push code from your machine to your GitHub account.

How to Connect Git and GitHub

Go to your Command Line or Terminal and enter the following command:

git config — — global user.name “Your name here”

git config — — global user.email “your_email@example.com”

Make sure to replace your “Your name here” with your actual name, and “your_email@example.com” with the email address you used to create your GitHub account.

Next, we’re going to set up an SSH, so that changes made on your local code file will be reflected on your GitHub account. To do this, open your command line or terminal, and enter the following code, replacing the email with your GitHub email.

ssh-keygen -t ed25519 -C “your_email@example.com”

This line of code will generate a key, which you’ll be asked to save to a local file, press enter, this will accept the default location. Make sure to remember where you save this file. In the file location open the id_ed25519.pub with a text editor (not a word processor) and copy the key generated, you’re going to need it shortly.

Next, a prompt will appear where you will be asked to enter your GitHub passphrase (login password).

For GitHub documentation on this process, click here.

Now, head over to Github and click on profile in the top right corner of your screen.

Click on Settings and then SSH and GPG keys. Click on New SSH keys. Give the key a title and then enter the key saved in the file.

Finally, click on Add SSH key and you’re done, Phew!

You can breath, you’ve linked your offline Git to your online GitHub profile. To confirm everything is working as expected, go to your command line or terminal, and type the following command:

$ ssh -T git@github.com

You should get a message that says,

Hi username! You’ve successfully authenticated, but Github does not provide shell access.

This is a sample screenshot of what the entire process looks like end-to-end.

If you’re not totally exhausted after that, we’ll discuss how to work with files on your computer, and then use Git to sync them to your GitHub profile.

Creating Repositories on GitHub

The first step to working on GitHub is to create a repository. On the top right corner of your GitHub page, click on the + button and then click New repository.

Fill in the name of the repositories, you can make it private or public. By setting it to private, you specify who you want to have access to your repository. If it’s set to public anyone can access the repository, and your code. Also, add a readme file and the license. You can use the MIT license. When you’re done click Create repository.

In the example below I have created a new repo called demo_repo, if you’re following along, you should have something like this on your screen.

GitHub demo repo

Working with files using Git Commands

Now that we have created a repo online we can download it to our local machine using the command git clone. To do this, go to your GitHub page, click on the Code drop down and then SSH clone. Copy the url link as shown in the image below.

To save the repo on your computer, navigate to a folder of your choice (I named mine gitProject) and type in the following command:

git clone insert_your_github_link

If successful, you’ll see that code repo will be downloaded to the folder you currently working in. Make sure to navigate into the specific repo after it has been downloaded using the cd command.

Note: You can clone (download) any open-source project on GitHub to your computer and work with it locally with this same technique.

Now, let’s add a file to our repo. Using a code editor, create a Python file, main.py, and then add the code as shown below:

Inside your command line or terminal, you can check the status of file changes with the command git status, this command is used to check which file Git is tracking. Make sure that you are working in the file location where the repo was cloned, and enter the following command.

git status

As seen in the screenshot above, Git is not tracking the new file we created in the folder, main.py. To ensure git tracks the file, you use the git add command.

git add name_of_file

Now, any changes made on the file will be tracked.

The command, git commit, is used to save to your code file. It’s good practice to regularly commit your code. Although Git tracks the code file with the git add command, it does not save changes until the git commit command is used.

The command flow is shown below.

git commit -m ‘A description of the latest changes’

-m stands for message. Git allows you to add a message or description to each commit you make. This is great for helping other developers understand what you were thinking or tyring to do at the time of the commit.

Now, let’s edit the file slightly and commit the change. To check that git tracks the file, let’s edit main.py. I have added a comment at line 3 below.

Then, we run the git commit command again to save the changes.

As seen, the commit has been made on the main branch.

Now that we have a code file, and we have finished working with it, we can upload the latest version of the file to GitHub using the git push command:

git push

If successfully, your GitHub profile will reflect the new changes made on the file. Take a look:

Summary

If you made it through this article, congratulations, now take break! It covered a lot, and chances are that you will need to read it again and repeat, several times, the instructions. But you have learned a lot. Using Git, and an online VCS is something that every competent developer needs to know how to do. And now you know:

  • what version control is
  • how to work with Git and GitHub
  • use git commands such as git status, git add, git commit and git push

If you have any questions, please leave them in the comment section and I’d do my best to answer them.

Finally if you would like a free guide to setting up Python, click here.

I hope you found this article useful.

Tony.

--

--