A beginner’s guide to Git & GitHub

M.V. Thanoshan
Sep 12 · 10 min read

What is Git?

Git is a free, open-source version control software. It was created by Linus Torvalds in 2005. This tool is a version control system that was initially developed to work with several developers on the Linux kernel.

This basically means that Git is a content tracker. So Git can be used to store content — it is mostly used to store code due to the other features it provides.

Real life projects generally have multiple developers working in parallel. So a version control system like Git is needed to ensure there are no code conflicts between the developers.

Additionally, the requirements in such projects change often. So a version control system allows developers to revert and go back to an older version of the code.

A branch system in Git allows developers to work individually on a task (Ex:- One branch -> One task OR One branch -> One developer). Basically think Git as a small software application that controls your code base, it you’re developer.

Figure 1: Shows how Git works as VCS.

Git Repositories

If we want to start using Git, we need to know where to host our repositories. Repository (shortly “Repo”) a project that contains multiple files. In our case a repository will contain code-base files. There are two hosting platforms to host our repositories. One, Online (On the cloud) and second, Offline (Self-installed on your server). There are three most popular Git hosting services. They are GitHub (Owned by Microsoft), GitLab (Owned by GitLab) and BitBucket. We’ll use GitHub as our hosting service.

Before using Git we should know why we need it?

1. It makes it easy to contribute to your open source projects
To be honest, nearly every open-source project uses GitHub to manage their project. Using GitHub is free if your project is open source and includes a wiki and issue tracker that makes it easy to include more in-depth documentation and get feedback about your project. If you want to contribute, you just fork a project, make your changes and then send them a pull request using GitHub web interface.

2. Documentation
By using GitHub, you make it easier to get excellent documentation. Their help section and guides have articles for nearly any topic related to git that you can think of.

3. Integration options
GitHub can integrate with common platforms such as Amazon and Google Cloud, services such as Code Climate to track your feedback, and can highlight syntax in over 200 different programming languages.

4. Track changes in your code across versions
When multiple people collaborate on a project, it’s hard to keep track revisions — who changed what, when, and where those files are stored. GitHub takes care of this problem by keeping track of all the changes that have been pushed to the repository. Much like using Microsoft Word or Google Drive, you can have a version history of your code so that previous versions are not lost with every iteration. It’s easy to come back to the previous version and contribute your work.

5. Showcase your work
Are you a developer and wishes to attract recruiters? GitHub is the best tool you can rely on for this. Today, when searching for new recruits for their project, most companies look into the GitHub profiles. If your profile is available, you will have a higher chance of being recruited even if you are not from a great university or college.

Now we’ll learn how to use Git & GitHub

a. GitHub account creation

To create your account, you need to go to GitHub website and fill the registration form.

b. Git installation

Now we need to install Git tools on our computer. We’ll use CLI to communicate with GitHub.

For Ubuntu:

  1. First, update your packages.
$ sudo apt update

2. Next, install Git and GitHub with apt-get

$ sudo apt-get install git

3. Finally, verify that Git is installed correctly

$ git --version

4. Run following commands with your information to set a default username and email when you’re going to save your work.

$ git config --global user.name "MV Thanoshan"
$ git config --global user.email "example@mail.com"

Working with GitHub projects

We’ll work with GitHub projects in two ways.

Type 1: First creating the repository , clone it to PC and work on it

Type 1 is about create a totally fresh repository on GitHub, clone it to our computer, work on our project and push it back.

Create a new repository by clicking “new repository” button on the GitHub web page.

Pick a name for your first repository , put a small description, check the ‘Initialize this repository with a README’ and click on the “Create repository” button.

Well done!. Your first GitHub repository is created.

Your first mission is to get a copy of the repository on your computer. To do that, you need to “clone” the repository on your computer. Clone: Taking a repository that’s on server and cloning it to your computer just like downloading it. On the repository page, you need to get the “HTTPS” address.

Once you have the address of the repository, you need to use your terminal. Use the following command on your terminal. When you’re ready you can enter.

$ git clone [HTTPS ADDRESS]

This command will make a local copy of the repository hosted at the given address.

Output message of “git clone” command.

Now, your repository is on your computer. You need to move in it with the following command.

$ cd [NAME OF REPOSITORY]

As you can see with the above picture, my repository name is “My-GitHub-Project” and this command made me go to that specific directory.

NOTE: When you clone, Git will create a repository on your computer. If you want, you can access your project with the computer user interface instead using the above ‘cd’ command on the terminal.

Now, in that folder we can create files, work on them and save them locally. To save them in remote place — GitHub, we have do a process called “commit”. To do this, get back to your terminal. If you closed it, like I previously stated use the ‘cd’ command.

$ cd [NAME OF REPOSITORY]

Now, in terminal, you’re in your repository directory. To “commit”, 4 steps are required. These four steps are called: ‘status’ , ‘add’ , ‘commit’ and ‘push’. All the following steps must be performed within your project.

  1. “status”: The first thing you need to do once your work is to check the files you have modified. To do this, you can type the following command to make a list of changes appear.
$ git status

2. “add”: With the help of the change list, you can add all files you want to upload with the following command,

$ git add [FILENAME] [FILENAME] [...]

In our case, we’ll add a simple HTML file.

$ git add sample.html

3. “commit”: Now that we have added the files of our choice, we need to write a message to explain what we have done. This message may be useful later if we want to check the change history. Here is an example of what we can put in our case.

$ git commit -m "Added sample HTML file that contain basic syntax"

4. “push”: Now we can put our work to GitHub. To do that we have to ‘push’ our files to Remote. Remote is a duplicate instance of our repository that lives somewhere else on a remote server. We must know the remote’s name (Mostly remote is named origin). To know that type the following command.

$ git remote

As you can see the above image, it says that our remote’s name is origin. Now we can safely ‘push’ our work by this following command.

$ git push origin master

Now, if we go to our repository on GitHub web page, we can see the sample.html file that we’ve pushed to remote — GitHub!.

NOTE: Sometimes when you’re using Git commands on the terminal, it could lead you to the VIM text editor (It’s a CLI based text-editor). So to get rid of it, you have to put

:q

and ENTER.

Pulling is the act of receiving from GitHub.

Pushing is the act of sending to GitHub.

Type 2: Work on your project locally then creating the repository on GitHub and pushing it to remote.

Type 2 is about make a fresh repository from an existing folder on our computer and send that to GitHub because a lot of cases you might actually have already made something on your computer that you want suddenly turn into a repository on GitHub.

I explain you guys with a Survey form web project that I’ve done earlier that wasn’t added to GitHub.

As I already told before executing any Git commands, we have to make sure that we are in the correct directory in the terminal.

By default, any directory on our computer is not a Git repository but we can turn it into a Git repository by executing the following command in the terminal.

$ git init

After converting our directory to a Git repository, The first thing we need to do is to check the files we have by using following command.

$ git status

So there are two files in that directory that we need to “add” to our Repo.

$ git add [FILENAME] [FILENAME] [...]

NOTE: To “add” all of the files in our Repository we can use the following command,

$ git add .

After staging area(Add process) is complete, we can check whether the files are successfully added or not by executing the $ git status

If those particular files are in green color like the below picture, you’ve done your work!.

Then we have to “commit” with a description on it.

$ git commit -m "Adding web Survey form"

If my repository started on GitHub and I brought it down to my computer, a remote is already going to be attached to it(Type 1) but if I’m starting my repository on my computer, it doesn’t have a remote associated with it, so I need to add that remote (Type 2).

So to add that remote, we have to go to GitHub first. Create a new repository and name it whatever the name you want to store in GitHub. Then click the “Create repository” button.

NOTE: In Type 2, Please don’t initialize repository with a README file, when creating a new repository on the GitHub web page.

After clicking the “Create repository” button you’ll find the below image as web page.

Copy the HTTPS address. Now we’ll create the remote for our repository.

$ git remote add origin [HTTPS ADDRESS]

After executing this command, we can check whether we have successfully added the remote or not by the following command

$ git remote

And if it output as “origin” you’ve added the remote to your project.

NOTE: Just remember we can state any name for the remote by changing the name “origin”. For example:

$ git remote add [REMOTE NAME] [HTTPS ADDRESS]

Now, we can push our project to GitHub without any problem!!

$ git push origin master

After completing these steps one by one, if you go to GitHub page and you can find your repository with the files!

Conclusion

So, Thank you everyone for reading. I just explained the basics of Git and GitHub. I strongly encourage you guys to read more related articles on Git and GitHub. I hope this article helped you all.

Thank you.

Happy Coding!

M.V. Thanoshan

Written by

#BelieveInYourself #BeYou and #BeUnique!

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade