What is Git? How does it work?

A Brief introduction about popular version control system -> Git.

4 min readMay 21, 2021

--

When I was in my undergraduate graduation years, I always used to wonder how these giant firms like Facebook, Twitter, and Microsoft maintain their codebase as they have thousands of employees working on different projects with hundreds of thousands of code changes every single day.

How do they get to know about each other changes? How do they communicate in different countries for the same project? what if they miss someone’s change? Just like me, many others had the same question. Nowadays I see some of the students/freshers are still unaware of this process.

This article is mainly intended to answer those questions, I will be trying to explain the version control systems, codebase management, and these concurrent changes and how they sync in very simple terms. Let’s get started…

What is this Git?

So in a nutshell Git is a version control system, that records changes to a file or set of files over time so that you can recall specific versions later. Got confused? 🤔 It basically keeps track of changes you made in your codebase at a given specific time(Version), which allows you to merge other developer’s code(version) in your code(version) leading you to a final version.

Many of you may get confused and think GitHub is Git, but no this is not true, GitHub is a repository hosting site that allows developers across the globe to host their code publically or privately. Whereas Git is software that allows you to control the versioning of your codebase. Git as the software comes in 2 forms CLI (command-line tool) and GUI. Both of the options work the same no matter what you choose, it is just about your preference but make sure before using Git you understand the basic flow and commands.

In this article, I will be using Git CLI to demonstrate to you the basic commands and their uses, make sure you have installed them in your system.

Basic terms that you should know.

Repository

This is a directory where you keep your code, it could be on a remote server or your local machine. The most basic thing you will be required to use Git is a repository there are 2 ways you can create a repository,

  1. You can take a local directory that is currently not under version control, and turn it into a Git repository, or
  2. You can clone or download an existing Git repository from elsewhere.

to make a new Git repository you can simply create a new folder and do git init, copy and paste the following commands on your terminal

$ mkdir dummyRepo && cd dummyRepo 
$ git init

This will make your dummyRepo directory an empty git repository. If you wish to use some existing repository you can simply type the following command

This will clone or download an existing repository from a remote server.

Branch

A branch is a pointer pointing to a change that you have made in a repository, a branch can be remote or local. If you have followed the previous step and if you type the following command in the terminal

you will see an output like this, the master is nothing but a branch that you have.

Commit

As I said earlier git keep track of the changes you have made to a codebase, this commit is responsible for recording your change to the repository. It simply records whatever changes you have committed to your given repository. Let's commit a change, create a new README.md file and commit it,

$ touch README.md 
$ git add README.md
$ git commit -m "Initial commit

There could be many changes you may have made in the code, you may not want to record all of those changes, for that you can use the git add command followed by the file name that you need to commit. Git add puts the file in a waiting area called a staging area.

That means when you perform a commit all of those files in the staging area(staged changes) will be committed. Now after you have decided what needs to be committed, you can do git commit it requires an option m that is a commit message and it is mandatory.

Push & Pull

Till now you have created a repository, added a file, and committed that file, but all changes you have made are on your local repository this needs to be transferred or pushed to a remote server where other developers see your changes and integrate them with theirs. To do this you can use the following command

$ git push origin master

This will push all your locally committed changes to the remote repository. Now, this is about the changes that you have made. Now, if you want to take some other developer’s changes into yours, you can use the git pull origin command. followed by the name of the remote branch.

$ git pull origin master

This command will pull all of the changes available in the remote branch master and merge them into the local branch. Then you can do your changes and follow the process to keep going.

This is the basics of Git that should be enough to get you started, please stay tuned for more of the article about the advanced feature that Git offers to the developers. Please do tell in comments how this article is? you can also get connected via Twitter.

Originally published at https://adarsh-thakur.hashnode.dev.

--

--