Git Introduction for Beginners

(Part 1)

Sathira Basnayake
CodeX
5 min readAug 12, 2021

--

Hi, I’m Sathira Basnayake and we will take a good look at basic concepts of Git in this article. Git is basically the most popular version control system in the world. Version control is used to track and manage changes of our projects and Version Control systems are software tools to do that task.

With Git we can record the changes with our code overtime in a special database called repository and we can see what changes are done and if we screw something we can easily revert the changes and come into a previous stage. Without Git we have to save multiple copies of our projects and manually manage them. If we work as a team we will have to exchange codes with something like email and manually merge them. But with Git we can easily work as a team and keep track of our code. I think now you have a good idea about why we must use Git in our projects.

Git is a distributed version control system which means every team member has a local repository in there machines unlike in centralized systems. So know we get the problem why git is the most used version control system? It is because git is free, open source, fast, scalabe. We can use git in the command line as well as in GUI. In this article I’m going to use the command line method.

There are several methods to install git in our machine and here is a very good guide. You can refer it and successfully install Git in your machine regardless of the OS. After you installed Git in your machine you can use Git to a new project or an existing project.

Now we will look into how do we initialize a new Git repository. Navigate to where you want to start the project and enter the git init command.

Once you enter this command a new git repository will be created alone with .git file and new master branch(we will talk about the branches in the future). This .git sub directory is hidden because it contains the meta data and we don’t have to touch it. To turn an existing project into a git repository we can simply run the same command in the directory which contains the project. And also we can do like this.

Now we have talk about how we can initialize a git repositary locally into a existing project or for a new project. Now we will talk about remote repositories to get the big picture and understand the full workflow. When we talk about remote repositories the GitHub comes into play.

GitHub is basically a cloud base online database which allows to keep track and share Git version control projects. So we can push over local repository to GitHub which is in the cloud and share and manage it with our teammates. That is how actually we use version control systems to work as a team. So once we push our local repository into GitHub it contains the remote repository which can be access and manage by our other team members. And you may be wondering is GitHub is the only online platform which can do this. No , there are other platforms like Bitbucket which does the same. But we will go with GitHub in this article.

But what if a project is already in GitHub central repository and we want to take that project int our local machine. That is where git clone command comes into the play. We can only run this command once and after that the user gets a working copy and he can use git to manage the version control locally.

I think now is the best time to get a better idea about the workflow.

Git workflow

As we can see in the above image(Git commands are shown with arrows and we will discuss those one by one) our local directory has 3 main parts.

  • Working Directory
  • Staging Area
  • Local Repository

In here the working directory is the directory which we create the .git sub directory. That is where we build our project. After some changes in our code we have to add the modified files into the staging state. To do that we use add command.

We use git add . to add al the changes int staging area in one go. After we add the changes into our staging are we can again review over work before committing. Once we have add all the modified files and we are happy we can commit the snapshot into our local repository. This staging area allows us to review our work before committing. To commit the changes into the local repository we use commit command.

In here when we do the commits we have to give a very meaningful message so if we were to check the commit history for some reason we can do it easily. Evey commit has a unique id and some meta information and the complete snapshot. So if we want to go to a previous state we can do that.

Now we will talk about how can we configure our local repositories in sync with remote repositories. For that we have to do some basic configurations.To do that we have to add a remote repo URL to your local git config

This command will map remote repository at to a ref in your local repository under . Once you have mapped the remote repository you can push local branches to it.

By using this command we have map a remote repository to our local repository. Now we can push local branches to the remote repository by using git push command.

After that when we want to get the changes done in the remote repository to our local repository we use git pull command. It immediately updates the local repository to match that content. Actually what git pull do is executing two git commands. Those are git fetch and git merge. As a good practice we always have to do a git pull before we push over modifications to the remote repository.

After we do this git pull and if there is conflicts we have to handle those manually. In this article we are not going to discus about merge conflicts.

Now I hope you all have a good basic idea about Git and GitHub and how to manage a project with Git. In this article we have discussed about eight git commands. Those are git init, git clone, git add, git commit, git push, git pull, git remote add. These are the basic commands and there are lot more. We haven’t discussed about git branches, merge conflicts , GitHub pages, GitHub actions kind of advanced topics in this article. We will discuss those topics in the future articles. If you have any suggestions or criticism feel free to contact me or you can drop a comment. Thank You very much and stay safe .

--

--

Sathira Basnayake
CodeX

I’m a third year undergraduate student in Faculty of Engineering University of Peradeniya. My specialization field is Computer Engineering.