Getting started with Git and GitHub: Beginner’s guide

What is Git?

Ufuk Emre Yücetürk
9 min readMar 26, 2022

Git is a version control system. If we explain in detail; Version control system (VCS) is an approach that deals with the management of the status of a set of documents over time, which consists of many documents, whose contents, numbers and relationships to each other are constantly changed, but still develops together, and software that facilitates change management processes of document sets in the computer environment with this approach. is the given name. I know it’s a bit complicated, let me explain it in a simpler way for you; In its simplest definition, it is a system where you can save the changes you made while developing your project step by step and if you want, you can store it in a remote repository on the internet. VCS also makes it easier for you to work on a project as a team.

In this article, I will talk about ways to use Git. If Git is not installed on your computer, you can find how to install it here.

1- Creating local git repository

Creating a Git repository is actually very simple. No matter how complex your project is, you can handle it with a single line of code. All you have to do is open terminal and go to your project’s root directory, then enter the “git init” command. It does not matter what is your project name. Here I am using named Test in my Projects folder. So it’s need to looks like that.

Some IDEs (ex: Android Studio) may notice Git settings and tell you that you need to configure Git project settings. These plugins in IDEs can make your job easier, but there is nothing in this article for any IDE.

This process should create a hidden folder named .git in the root directory of your project. Now enter the ‘git status’ command. This command will give you information about the status of your repository. For example, if the changes made are not saved or added, you will be informed here. At the same time, you can see many details such as deleted and added files.

A terminal screen that git status

Now let’s add all our files to saving list and commit them all. To do this, enter the ‘git add .’ command. Normally you specify the file you want to add at the end of the ‘git add’ command. The dot we use at the end of our command means add all files.

Enter the command ‘git commit -m “first commit”’ for the commit process. As soon as you enter this command, you will see lines stating that all the files in your project have been added one by one.

Now, if you enter the ‘git status’ command again, you will see a text on the screen showing that no changes have been made since the last save.

You now have the Git repository for your project on your computer. You can do whatever you want on your project. Because if you break something, it will now be possible to restore your project to its former working state with a single line command.

2- Basic git commands and usage

Now let’s look at the most used git commands and explain them with a few examples.

Go make a few changes to any file in your project and then use the ‘git status’ command to see how those changes are reflected in your repository. I changed the index.html file in my project and the result was like this.

In the command line you can see the list of files that have changed since the last save. What we’re going to do now is add the files to the recording and then do the recording. You can do this by ‘git add .’ and ‘git commit -m ‘second commit’, as we just did. But this time we will do it by combining the add files command with the save command. Enter the command ‘git commit -am “second commit”’ on the command line. In addition, the letter ‘a’ we use here will add all the modified files to the recording while recording.

View changes

Before committing, you can use the ‘git diff’ command to see what are the unsaved changes (lines added and removed). If changes have been made to a long file, you need to proceed line by line by using the ‘enter’ key. If you do not want to come to the end of the file, you can exit by pressing the ‘q’ key on the keyboard.

Reverting changes made

If you do not want to save the changes you have made (for example, if the last changes caused the project to break down and it takes too long to restore it), you can reset your project to the last saved state with the ‘git reset — hard HEAD’ command.

You must be careful when using this command. Because there is a possibility that you accidentally delete the changes that you do not want to lose. Therefore, before using this command, check thoroughly whether there is a part of the changes that you do not really want to delete.

3- Placing your project in a remote repository

There are many platforms that provide remote repository services. The main one is GitHub. GitHub is a web-based storage service for software development projects that use Git as their version control system. I will put my project in remote repository using GitHub

To move our repository to the remote repository, we must first create a remote repository. What we need to do now is to create a new repository by going to the repositories tab and pressing the new button there.

A very simple form will appear in front of you. At the top of the form, there are sections where you can select the account that will be the owner of the repository (if you work with more than one account on GitHub) and enter the name of the repository. Just below them are sections where you can enter a simple description of the project and choose whether the project is public or not.

At the bottom of the form, there is an option called “Add a README file”, we will not use it. There are two options just below it. Their brief description is as follows:

  • .gitignore: This file contains files within the project that you do not want to be in the remote repository. In general, the main purpose of use is not to put the files that are created when the project is compiled to the remote repository. You can also prevent the files connected to the IDE you are using from being placed in the remote storage. Because these files will be created when you compile on your computer. In addition, in each project type (web, ios, android), the types of files that are unnecessary are different.
  • License: The license also includes the license terms that they will be responsible for if the project you put in the repository is used by others.

We’ll skip those for this project, but I highly recommend using .gitignore in the future projects.

Create your repository by pressing the Create repository button. After the repository is created, you will see a section on the page that will appear, explaining how you can put your project in this repository. Since we already have a git project, we will only upload our project to this repository. It has already been described in this section on the page.

Check the changes made in your project again with the git status command, and if there are any unsaved changes, save them with the git commit -am “commit message” command. And now you can map your project to your remote repository with git remote add origin https://github.com/uemrey0/Test.git command as shown on GitHub page. (Of course, you should use the URL of your own repository when using this command.) In the meantime, you will be asked for the username and password for your github account. If you enter this information, your project will be matched with the remote repository.

However, you won’t be able to immediately see your project in your repository. You should also use the git push -u origin master command to push your project to the repository. After entering this command, you will see that the installation process is done on the command line. Meanwhile, your project will be uploading to the remote repository, and as a result, your command screen should display a result similar to this.

Now if you go to your project page on GitHub, you will see the files of your project instead of the messages just telling you how to add your project.

4- Saving changes to remote repository

We now have a local git project on our computer and a corresponding remote repository remotely. You can browse through the folders in GitHub to look at your files. Now we will make changes to the project and save it to the remote repository.

In my own project, I change the title in my index.html file and I can see the change in the terminal.

But if you open the file you made changes to from the GitHub page in your remote repository, you will see that there is no change there.

Now commit the change with git commit -am “first change” command. At this stage, there will still be no changes to your remote repository. Any changes you make remain in your local git repository until you use the push command. If you want these changes to take effect on the remote repository, use the git push origin master command.

After this command, if you open the file you made changes from GitHub page, you can see that the changes are reflected here as well.

Here, you can see how many people have made changes to the file you are looking at, who made the last change and when, and what the message of this change is.

If you click on the first change text written at the end of the line in the line with the profile photo, you can see what changes were made in that record. (In the same way, you can see the list of records made on this file by clicking the History button at the top right)

Finally

As a result, we have now started to use git in our project. Now you know how to use git in your project, how to save changes, how to create a remote repository and import your project there.

These are just simple git usages. I hope I didn’t scare you. You can easily manage your project with this methods. Keep researching. I plan to publish a few more articles on more detailed topics about git. Stay tuned!

--

--

Ufuk Emre Yücetürk

Software Developer, Engineering Student | Seeking Software Developer Experiances | PHP, JavaScript, Python, C#, Java