What is Git and Why is It So Important? | by Soner Yıldırım | Towards Data Science

Linus Torvalds created the Linux operating system and he made it publicly available after a while. His initial purpose was to get comments and feedback about Linux from other people.
People started contributing to and getting involved with Linux. The contributions were not only in the form of code but also ideas and comments. The community working together on Linux made it an open source project.
Linux is in millions of computers and powering billions of devices. You may be wondering why I’m writing about Linux although the title is about Git.
Git was also created by Linus Torvalds in the process of developing Linux. Many people were involved in the development of Linux by making small or large contributions.
After some time, it became really hard to manage and maintain all the changes and updates. That is the reason why Git was created.
Git is a version control system. It maintains a history of all changes made to the code. The changes are stored in a special database called “repository”, also known as “repo”.
Two main advantages of using Git at software development:
Tracking the changes and updates. We are able to see who made which changes. Git also provides when and why a change was made.
Allowing to work collaboratively. Software development projects usually require many people to work together. Git provides the developers with a systematic way of doing that. Thus, the developers focus on the project instead of extensive communication sessions between the other developers.
There are other version control systems such as Subversion, Mercurial and so on. However, Git is the most popular one. Some of the reasons that make Git the preferred one are:
Speed
Scalability
Being free and open source
Assume I have project files in my project directory. I make some changes on a file and want to add the updated file in the git repository.
The first step is to add the updated file to the staging area. After review, it is committed to the git repository.
The word “commit” means a lot and it is a fundamental operation in Git. Let’s delve into what happens when we commit a file to the repository.
Git takes a snapshot of the project and stores it in the repository along with some other identifying information.
Each time we commit, Git records the information above. Thus, we have a highly useful history of the project.
When we commit a file, we provide a message that describes the change. This message should be as informative as possible in order to build a decent history.
Note: The advantage of saving all these information for every change is that we can easily revert back to an earlier version in case of a mistake.
You may be wondering why Git is taking a snapshot of the entire projects instead of just recording the changes. When there are lots of changes, it is easier to record a snapshot instead of the changes. Besides, when going back to a previous version, it becomes very complicated to track down all the modifications.
One issue with recording the snapshot of entire project might be the space. We don’t have to worry about it because Git is quite efficient in storing the snapshots. Git does not store the duplicate content and also compresses the content.
We have covered the workflow theoretically. Let’s also do a few basic practical examples.
The easiest way to use Git is through the terminal. You can also use some text editors and IDEs but terminal provides the most functionality.
I will first create new project directory.
$ mkdir newproject$ cd newproject
I’m now in the newproject directory. Let’s first initialize an empty git repository.
$ git initInitialized empty Git repository in /home/soner/Desktop/projects/newproject/.git/
The git repository is hidden so you need to run the “ls -a” comment to check it has been created.
$ls -a. .. file1.py .git
Note: The git repository is hidden because we are not supposed to touch it. It contains some implementation details. We do not care what is in the git repository. We just want to know it is there.
I have a python file in the project directory but it is not saved in the git repository yet. We will add it to the staging area and then commit to the repository.
$ git add file1.py$ git commit -m "Initial commit."[master (root-commit) d95fba1] Initial commit. 1 file changed, 11 insertions(+) create mode 100644 file1.py
The git add command adds the file in the staging area. The git commit command saves the file in the repository permanently. You can use “git show” comment to view the files in the git repository.
When we first add a file, it is in the staging area. We can check it with “git status” command.
$ git add file11.txt$ git status
The new file (file11.txt) is waiting to be committed to the repository. Let’s commit it and check the status again.
$ git commit -m "New file added."$ git status
Git is a very important tool used in software development because of the advantages it provides. Even if you are or plan to be a data scientist, I suggest to learn Git to a certain level.
What we have covered in the practical part of this article are just the very basic operations in Git. My goal is to get you familiar with the concept so you have an idea what Git is all about.
Thank you for reading. Please let me know if you have any feedback.