Creating your first repository on GitHub

Ronaldo B.August 1, 20227 min read

In this article

We have already talked on our Blog about Git, including learning how to install it on our computers. You can check this content at this link: Installing and Configuring Git on Windows, Linux, and Mac

Another topic related to Git is GitHub. But, what is GitHub? Is it the same as Git? We will answer these questions in this article.

What is GitHub?

The first important thing we always need to remember is that GitHub is not the same as Git. As we learned in another article, Git is a version control system that manages our code and the changes that occur in it. GitHub, on the other hand, is a platform where we can host the projects we create. This platform was created in 2008 and is now owned by Microsoft (its purchase by the Software Giant occurred in 2018). Each project added to GitHub is stored in a repository. We use Git to upload and download files from our project. Thus, we see that GitHub is not the same as Git, but it needs Git to exist.

GitHub, along with StackOverflow, is considered two of the major “social networks for programmers.” We can add the projects we develop to our profile, and thus the entire community of developers can access them. Therefore, we can say that GitHub can be considered our portfolio, and many companies may even ask for our GitHub profile when analyzing a job vacancy.

In this article, we will learn how to create an account on GitHub and also how to create our first repository.

Creating an account on GitHub

The GitHub website is github.com.

Right on the homepage, we will see a registration form on our right, where we can enter the username we desire, our email, and a password. We must fill in this data and click on the registration button, as seen below:

GitHub homepage

On the next page, we will start our registration process. We will visualize some type of verification, such as a captcha or a small game, to confirm that we are not robots, as seen below:

Validation on GitHub's website

The next step will be to choose whether we want the free or paid plan on this platform:

Plan options on GitHub's website

It may be necessary to validate your email using a code that will be sent to your Inbox. After these steps, our account on GitHub will be successfully created and validated!

We can now create our first repository!

Creating a repository

After logging into GitHub, we will see a button with the drawing of a plus sign (+) in the upper right part of the platform. By clicking on it, a dropdown menu will be displayed, and we will choose the first option, “New repository”:

Logged-in GitHub page with the option to create a new repository

We will be directed to a page for creating a repository. We need to enter the name of the repository we want and a description for it. Finally, we will click the “Create repository” button:

Repository creation page on GitHub

Just a detail: we also selected the checkbox to start the repository with a README file. This file will contain a description of our repository. This description can be short and concise. However, if we wish, we can add more information to this file. Large projects use the README file to even create documentation for the project, with links and tutorials.

After creating our repository, we will see the following page:

Repository page on GitHub

This page represents our repository, already hosted on GitHub. Quite simple, don’t you agree?

We definitely want to add more files to our repository. We could edit the files directly on the platform. However, if our project gets too large, it may become unsustainable to edit the files directly on the server. This is when Git comes into play. We can use this tool to download the code that is in the cloud, creating a local repository on our computer. Then, we will make changes to it locally and then upload the new version of the project to the cloud.

To do this, we will first download the repository. But, when using Git, we do not use the expression “perform a download,” but rather “perform a clone,” because we are actually going to clone the content of the remote repository to our local. For this purpose, we will use the command git clone and provide the link to our repository. We can obtain this information by clicking on “Clone or download” and choosing the copy option:

Clone or download option on GitHub

We will work with the command line from now on. It will be necessary to have Git installed for that. You can access the article where we teach its installation through the link at the beginning of this article.

If you wish, you can open your default Terminal or Git Bash on the Desktop. We will execute the following command:

git clone https://github.com/anthony1910/first-repository.git

Just remember that the address of your repository will be different from mine :)

This command will create a folder named first-repository, which will be our local repository. For our example, we will create a file named index.php inside this folder. Its content will be as follows:

<?php

echo 'This file is in my first repository';

This new file represents a change in our repository. We now need to add this new version of the project to the remote repository. We will use some other Git commands that will help us in this process.

The first one is git add. It adds our files to what we call the “staging area,” an expression that can be translated as “preparation area.” Adding files to this area shows our intention to include them in the remote repository. We will use this command in our Terminal:

git add index.php

Notice that we only specified the index.php file. However, in large projects, we will have many files. To avoid having to include one by one, we can specify the “--all” option, which adds all files to the staging area at once:

git add --all

After adding this file to the staging area, we need to describe the change we just made, documenting that the change was made by us. For this, we use the git commit command. It expects a parameter called “-m,” which represents the message for that commit. Let’s execute it with the command below:

git commit -m 'First Commit'

Notice that it is important for our message to be in quotes if it contains more than one word in its content. It is also interesting to note how the commit feature can be useful for us. If there is a good standard for commits and if their messages are well descriptive, we will have in our hands an excellent change log system, something highly desired in any company or project.

Now that we have provided a description for our change, we actually need to “push” the new version to GitHub. We will use the git push command for that. One parameter it expects is the name of the remote repository and the other parameter is the name of the branch we want to send. By default, we will use the command below:

git push -u origin master

In this context, “origin” is our remote and “master” is our main branch. Just a detail: we also use the “-u” option, which will create a tracking reference for this branch.

After executing this command, we will see the following result in our Terminal:

Result of the git push command on Terminal

Our push was successful, great!

Since this is the first commit of our project, no errors occurred. However, as the project grows and undergoes changes from various team members, we may encounter an error that will ask us to update our local repository with the content of the remote repository. To do this, we can use the git pull command. It has the same structure as git push:

git pull origin master

If even after executing this command the error continues, you can use an alternative command that we explain in this article: Git: failed to send commits to the remote repository.

Returning to our project, is this new file actually on our GitHub? If we go back to the page of our repository and refresh it, we will see the following:

Page of our repository on GitHub

See that our index.php file is already in our repository in the cloud, and the message of our commit is also on GitHub. Very good, everything worked out!

This is the process we use to keep our project always updated.

With that, we can now create our own projects or contribute to open-source projects. As we mentioned at the beginning, this could be our personal portfolio.

Would you like to read more about Git and GitHub on our Blog? Or is there any other topic you would like us to discuss here? You can send your suggestions by clicking this link: Suggestions

Until the next article :)

Keep learning with Hcode

Sign up for the course platform and follow the next blog guides.