Git — the superstar version control system

Written by dhkelmendi | Published 2017/07/28
Tech Story Tags: git | version-control | version-control-system | github | bitbucket

TLDRvia the TL;DR App

Being the most adopted Version Control System (VCS), Git makes contributing to open-source and teamwork as easy as a walk in the park. If you are a developer, you should definitely know it.

As software developers, many people tell us that we have to learn X or Y. This can quickly get overwhelming for new developers who are seeking for advice. The advice we get is not always right. However, there are cases when a technology is a must, and you will find that the majority of developers use it and advocate learning it. Version control systems (VCS) are such a technology. Everyone is using them (if not, they definitely should). Git is one of the most used version control systems. Let’s dig deeper into VCSs and Git and see why they are considered a must when developing.

The problem

If you have developed applications without using VCS, you most probably have had bad experiences synchronizing the files between the team members. I myself have had this problem, and I used to name files with a version number so that I differ between the files.

Manual Version Control, source: imgur

Besides looking bad, it also has many usability problems: finding the version you need, remembering the changes you made in each version, and sharing the files with the team. As a solution, there have been many systems which intend to make this process easier and safer. These systems are called Version Control Systems.

Version Control System(VCS)

For solving the mentioned problems, and many more, different types of VCSs were developed. Let’s talk about them briefly.

Local VCS

This type of VCSs enables users to store a history of the changes they made to their files locally, eliminating the problems regarding file history, but still leaving a big problem unsolved: the team collaboration.

Local VCS, source: Git

Centralized VCS

Centralized VCSs solve the problem of team collaboration by centralizing the history and storing the versioned files in a server, where clients can access the files and get the latest snapshot of the history. For a long time, this has been the go-to VCS. While it offers many advantages compared to Local VCSs, it still has a major issue, which is also found in Local VCSs: there is a single point of failure — if the central server fails, the whole file history is gone for good.

Centralized VCS, source: Git

Distributed VCS

Distributed VCSs bring all the good parts of the Centralized VCSs, and more. They eliminate the single point of failure by sharing the file history with each team member and not only get a snapshot, but get the full mirror of the repository. If any server dies, the history does not die with it, it can be restored from the clients, who have a full mirror of the repository (i.e. a full backup of the data). Being distributed, they offer a great possibility of developing in a non-linear manner and choosing one of several workflows.

Distributed VCS, source: Git

Git

source: codereviewvideos

Git is a distributed VCS which was born as a need of the Linux development team. It is an open-source project started by Linus Torvalds (who also started Linux).

Getting Started with Git

**Disclaimer**

This will not be an in-depth tutorial about git, it will rather be a short tutorial about the basic git commands.

Starting the work

  • If you are starting a new project, or have an existing project that you would like to add to git, run git init inside the project’s folder.
  • If you want to work on a project which is already on git, get the git repository URL, and run git clone UR.

Git directories

Git organizes your repository in three directories: Working Directory, which contains the files, Index, which is the staging area and HEAD, which points to the last commit.

Git add & commit

To send your changes in a file to the Index, you run git add fileName. After adding all the changes, if you want to send them to the HEAD, you run git commit. But, the changes are still in your local repository.

Pushing your changes

To push the changes that you’ve committed, you run git push origin branchName.

Branching (GitHub Workflow)

Branches, as the name suggests, allow developers to work on different features independently. They help developers organize their workflow with ease. One of the popular workflows is GitHub workflow, which suggests that each time you want to work on a feature, you create a branch, commit the changes, and then merge the branch to the master branch. You create a branch by running git checkout -b branchName, which is a shortcut, explained here.

Staying up-to-date

Since git gives each person involved in the project a local copy of the repository, this copy has to be updated after someone pushed to the remote repository. You do this by running git pull. At times, this process is where problems may rise. As many people may be working on the same files, there may be some conflicts, which you will have to resolve. Resolving a conflict can be explained as telling git which changes to keep. You will then have to add and commit the resolved conflicts.

Log

To view the history for your repository, run git log.

Discard changes

If you have made some changes that you are not proud of, you can discard them by running git checkout fileName; you can discard ALL the changes by running git checkout ., which you should only use when you are sure that you want to discard all the changes. If the changes are in the Index (i.e. you ran git add fileName), you can run git reset HEAD -- <directoryName>, which will remove the directory from the staging area(Index), but it will not discard the changes (you will have to run git checkout).

Advantages

  • FOSS(Free Open-Source Software)

Git is an open-source and free software, meaning that not only you are permitted to use it to fulfill your needs, but you can also configure it to your preference.

  • Fast and small

Being distributed, it does not interact with the central server all the time, resulting in a very fast and efficient VCS. Although it mirrors the central repository locally, the clients’ file size is quite small

  • Distributed VCS

Being a Distributed VCS, Git ensures that no data is lost, as the central repository is mirrored in each client, which enables restoring files from the client to the central server.

  • Secure

For naming and identifying objects in its database, Git uses SHA1, which is a cryptographic hash function. Each file and commit has a checksum, which ensures that there is no change from the Git database without Git being aware of it.

  • Flexible

This advantage comes as a result of Git being a Distributed VCS. Git enables users to choose whatever workflow they prefer, be it centralized, feature, git, forking, or other.

Git hosting services

The most popular service is GitHub (find me on GitHub: DhurimKelmendi) where you can find many interesting open-source projects. Alternatives to GitHub include Bitbucket, GitLab, etc.

Helpful learning resources

There are many awesome sites that help you learn git. Here are some of them:

The next step

After learning git, make sure you contribute to open-source projects and help the world while coding.

Feel free to email me if you have any questions, challenging opportunities, or you just want to say hi.

Finally, be sure to follow me so that you do not miss any new stories I publish.


Published by HackerNoon on 2017/07/28