Git 101: Everything you need to know to get started with Version Control
By: John Greenfield
Tags: Git, Programming, Teamwork, Software Development, Github

Create a repository on GitHub

Setup a free account on GitHub

The first thing we want to do is hope on over to GitHub and sign up for a free account if you haven’t already. Once you’re registered and signed in, take some time to look around and explore things a little bit.

My GitHub homepage

Create a Repository

Now that you have an account, it’s time to create your first repository. You may be asking yourself, but what is a repository? Simply put, it’s a place where you store and organise a single project which can includes files like images, text documents and HTML files, data sets and so on.

Upper right corner menu

We now know what a repository is, so let’s get to creating one. Look in the top right corner of the page, and you should see three icons; a notification bell, a plus sign and your profile image. Click the plus sign, which opens up a small menu and select New repository.

Create a new repository form

You should now see a page that looks something like the image above. Here we can create our first repository by filling in the name, description and setting our privacy settings. Let’s call this repository hello-world like all good first projects. Next, type a few words to describe this project, for example “My first GitHub project”. Set the privacy to Private for now, and then tick the box that says “Initialize this repository with a README”. This will do exactly what it says on the tin, and ensures we have a file already in the repository which allows us to clone it straight to our computer.

If you’ve looked through a GitHub project before, you’ll have noticed that they all have a README file that outlines what the project is, how to use it and sometimes troubleshooting advice.

Once you’ve filled in all the information, just tap on the Create repository button at the bottom of the page, and we have our first project! Congratulations.

You should now be looking at the repository page, with the newly generated README being displayed on the bottom half of the page. There’s isn’t a lot to it right now, but I recommend you take GitHub’s own Hello World tutorial which will walk you through the basics of the web interface and teach you how to manage a new branch, make changes to a file and push them to GitHub as commits, as well as how to open and merge a pull request by editing the README file. You can find the link below, and once you’re done with that, we’ll move onto the next section which is installing Git to your computer.

Install Git

I won’t be including instructions for Mac OS X, as usually Git comes preinstalled with XCode but also I’m unfamiliar with the environment itself. If you’re unsure, just search for something along the lines of “Mac OS X Git installation”.

Windows

For Windows, download the latest Git for Windows installer. Once it’s downloaded, open up the installer and follow the prompts, keeping the default options for now. Once done, click finish and open up a command prompt (Win key + r, then enter cmd and hit enter). We’re just going to quickly setup your username and email which will be associated with any commits you make. Enter the following two commands into the command prompt, replacing the name and email for your own.

$ git config --global user.name “John Greenfield”

$ git config --global user.email “john@johngreenfield.dev”

If that was successful, you shouldn’t see anything in response to hitting enter. There’s one final step, and that’s to install the Git credential helper on Windows. This little application which was created by Microsoft will store your credentials for logging into GitHub (as well as Bitbucket and other platforms that support pushing and pulling over HTTP).

Download the Git Credential Manager, follow the prompts and once that’s done, we’re good to move onto the next section. Let’s go!

Debian/Ubuntu

If you’re using Ubuntu or some other Debian based OS, then the steps for installing Git are really quite simple. Enter the following commands to install Git.

$ sudo apt update

$ sudo apt install git

Once that’s done, enter the following command to get the version of Git currently installed, and if it returns a value similar to my example, you’re good to go.

$ git --version

git version 2.27.0

One final step to finish configuring our setup, and that’s to enter your user name and email into the global settings which will be associated with any commits you make.

$ git config --global user.name “John Greenfield”

$ git config --global user.email “john@johngreenfield.dev”

Copy your repository

Great, so by this point you should have a hello-world repository hosted on GitHub (hopefully complete with a README file), and Git installed on your machine so that we can start making changes to our project.

In order to make changes to your repository, you need a way to copy or “clone” it onto your system. Cloning a repository creates a connection between the GitHub server, known as Origin, and your local system.

Before we begin, let’s prepare our local system for the incoming files. Navigate to your home directory if you aren’t there already. For Windows users, you should already be there and your terminal will look something like this:

C:\Users\John>

For Ubuntu/Debian users, just enter the following command to be taken to your home directory.

cd ~

As a developer, you’re likely to work in multiple repositories, and so we’re going to make a new folder to store all our repositories in. Enter the following command:

mkdir repos

Now navigate to that folder by entering the following:

cd repos

So now we have a new directory called repos in our home directory, ready to receive our cloned project. The next step is to open up our GitHub accounts in a browser, navigate to our hello-world repository and clicking the green “Clone or download” button as seen in the images below. You’ll see a pop-up that says Clone with HTTPS, and make sure to copy the URL given.

Hop on over to the terminal again, and enter the following command, where the URL is the one copied from the pop-up on GitHub:

If you installed the Git Credential Manager, you should now get a window pop-up asking for your GitHub login details. Enter them, and the terminal should say that it is Cloning your repository into a folder called hello-world. You can check this by navigating to the new folder. Make sure you are in the repos folder, then enter either dir for Windows users, or ls for Ubuntu users. The new folder should be there, so let’s enter it with the following command:

$ cd hello-world

That’s it for cloning, you’ve done it! Now let’s move on to the next section and try adding a new file to our repository.

Add files to your repository

Carrying on, we’re going to quickly put the name GitHub into a text file called platforms, and the check the status of our repository. While still being in the hello-world folder, enter the following into your terminal window:

$ echo GitHub >> platforms.txt

If nothing is returned after entering that command, then that means you did it correctly. Next up is to get the status of your local repository using the git status command. This will tell you about how your project is progressing in comparison to your GitHub repository. Enter git status into the terminal window and you should see something like that displayed below:

$ git status

On branch master

Your branch is up to date with 'origin/master'.

Untracked files:

(use “git add <file>…” to include in what will be committed)

platforms.txt

nothing added to commit but untracked files present (use “git add” to track)

So let’s run through what this is saying. First line, On branch master, means we are working on the master branch (which normally you wouldn’t, but we’ll cover that in more detail later on). The second line is the message stating your branch is up to date with the origin/master branch on GitHub. After that, it lists the currently untracked files and gives us some rather useful information. We want to add the file so that it will be included in the commit. Let’s try it:

$ git add platforms.txt

This has moved the changes from the working directory into what’s known as the Git staging area. The staging area is where all the changes are gathered before committing them to the official history of the project.

Let’s check the status again, and see what it says now.

$ git status

On branch master

Initial commit

Changes to be made:

new file: platforms.txt

Excellent, now you can see that the file is ready to be committed to the repository. Now we want to issue the commit command so that the snapshot we took of our changes can be added to the projects history. The two commands commit and add combined is essentially the basic workflow for all contributors. Enter the following code, with the -m option and enter a message between the quotation marks.

$ git commit -m "Initial commit"

There is now one final command we need to use to “push” our changes to the master branch on “origin”, which if you remember, is our hosted code repository on GitHub. Enter the command git push origin master, and the output should look something like the following:



Enumerating objects: 4, done.

Counting objects: 100% (4/4), done.

Delta compression using up to 8 threads

Compressing objects: 100% (2/2), done.

Writing objects: 100% (3/3), 284 bytes | 284.00 KiB/s, done.

Total 3 (delta 0), reused 0 (delta 0), pack-reused 0

To

a528bd8..ad592fe master -> master $ git push origin masterEnumerating objects: 4, done.Counting objects: 100% (4/4), done.Delta compression using up to 8 threadsCompressing objects: 100% (2/2), done.Writing objects: 100% (3/3), 284 bytes | 284.00 KiB/s, done.Total 3 (delta 0), reused 0 (delta 0), pack-reused 0To https://github.com/johngreenfield/hello-world.git a528bd8..ad592fe master -> master

Well done, your commits are now on the “origin” or remote repository, you can check that out by going over to your repository in your web browser, and clicking on the Commits tab.

Pull Changes from your Repository

Our next challenge is to edit the platforms.txt file on the website interface so that we can practise “pulling” update files to our local system. Head on over the your repository in your web browser, and click on the test.txt file. It should open up in a window similar to the image below.

Click on the pencil icon in the grey box to edit this file, then enter two more version control platforms such as “Bitbucket” or “Gitlab” as a list in the file. Scroll down to where you can see Commit changes, and enter a short message in the first text box and an optional extended description. Ensure Commit directly to the master branch is selected, and click the green Commit changes button.

That’s it! You’ve now updated a file directly on the master branch, so now the next thing we want to do is pull that updated file to our local repository. If it helps, just think of “pull” as the opposite process as “push”. The git pull command will merge the file stored on the “origin” repository into your local repository with one quick command.

In your terminal window, make sure you’re still in the repositories folder (looking something like C:/Users/John/repos/hello-world or ~/repos/hello-world) and enter the git pull --all command. You should see something like the following:



Fetching origin

remote: Counting objects: 3, done.

remote: Compressing objects: 100% (3/3), done.

remote: Total 3 (delta 0), reused 0 (delta 0)

Unpacking objects: 100% (3/3), done.

From

Updating a528bd8..ad592fe

Fast-forward platforms.txt | 2 ++++++++++++++

1 file changed, 2 insertions(+) $ git pull --allFetching originremote: Counting objects: 3, done.remote: Compressing objects: 100% (3/3), done.remote: Total 3 (delta 0), reused 0 (delta 0)Unpacking objects: 100% (3/3), done.From https://g ithub.com/johngreenfield/hello-world fe5a280..fcbeeb0 master -> origin/masterUpdating a528bd8..ad592feFast-forward platforms.txt | 2 ++++++++++++++1 file changed, 2 insertions(+)

Now just navigate to your local repository folder, either in the terminal or you can use a file explorer, and open up the text file. You should see a list of the three Git platforms we’ve talked about during this article.

I think that will do us for today, and that’s definitely a solid foundation for you to build upon. For a little bit of homework, try creating a git “branch” and edit a file that way. This will involve the git commands branch, checkout and merge as well as the commands we’ve already been over in this article.

If you have any questions, problems or issues then feel free to leave a comment and I’ll do my best to help you. I hope you found this useful, and good luck in your web development journey!