Git – an essential tool for developers
Git is probably one single tool that every software engineer – no matter their background – uses every day. In this article, we’ll explore why Git is so widely used and how it helps millions of people worldwide in their day-to-day work. Here, we’ll focus only on an overview of Git. If you want to learn some specific commands or examples of workflow, they will be described in the next article.
Why does Git even exist?
Git is an essential tool for every software engineer, but you don’t have to be one to take advantage of its features. Why does everyone use it? One of the definitions of Git, which states it is a “distributed version control system”. This gives us a clue about which aspects of our daily work it can help us with–managing versions and controlling the changes we make. What changes? Well, you can use it to version any file on your disc. However, Git is best suited to handle files in text format (source code, documentation, articles, etc.). In addition to tracking your changes, Git also makes our lives easier when we introduce some disruptive changes that may affect the functionality of our project. It ensures that the previous state is always a few Git commands away from us.
The short definition of Git also included the word “distributed.” It means that it has out-of-the-box features that can help you work with others on projects. However, with hosting services like GitHub or GitLab, it is mostly used as a centralized version control system.
This short introduction may be vague at this point, but it’ll all make more sense soon as we discuss each of Git’s features in detail.
Version Control System (VCS) – don’t lose anything
It’s a bit of simplification, but Git is like this powerful, manual version of the good old Ctrl-Z shortcut. The Ctrl-Z shortcut is good when dealing with a linear change history. In most cases, it keeps track of all your changes in a file, whether you want it or not. With Ctrl-Z, you can’t choose a specific point in your project’s history, and it’s dangerous to go back because your current work is not saved. Git solves this problem by letting you save versions of your project at different stages of development. These versions are called commits.
Commit – a single version of your project
Git only keeps track of your changes when you ask it to by using some of its commands. To keep track of that state in time, you have to capture the state of your file(s). This is sort of a “snapshot” of your file. This snapshot, in Git terms, is called a ‘commit’.
In practice, this commit, which represents the state of your project at a given point in time, is a version of your project. You can choose what changes should be grouped together into a single commit (version). While you should use this feature as often as possible, don’t commit all your lines of code separately. However, if you finish a standalone part of your work–even the smallest–you should commit it so it can be safely stored by Git.
There is no golden rule on what and when to commit. It’s a skill that you need to learn through practice. So, to sum it up, Git tracks your changes only in specific states of your file called commits. You ask Git to do it through your CLI. In addition to your changes, every commit has a unique identifier called commit hash and a descriptive message, which helps easily identify what has changed in that version. You’re responsible for writing that message. Commits are versions of our projects, but those versions are not in a void. To organize your commits in some meaningful way, you need to understand one more topic: Git branches.
Branches – a way to organize your commits
A second key feature of Git is its ‘branching’ system, which ensures that even the most complex history of changes can be easily navigated. Git is often praised for this specific features. But what is this mysterious ’branch’? Simply put, a branch is a separate line of development of your project, whether it‘s a book, reminder app, or anything else. Projects often have a primary branch called the main and separate branches where you can introduce some new functionality to your app. For example, you might create a branch and call it ‘feature/integrate_db_connection’. Branches are also used to create releases of your project, such as branch release/1.10.
Moreover, Git lets you combine your changes from feature branches into the main branch through the process of merge or rebase. This especially comes in handy when cooperating with others on different parts of your project. Each contributor can create their branch and introduce new functionality as a separate line of development. In the end, others can review the change, decide whether to incorporate it into the main branch, and determine if some additional tweaks are needed.
The image below shows an example of a Git project history with multiple branches. As mentioned earlier, Git is mainly used as a centralized VCS with the main repository hosted on some service. Now, let’s look at how to cooperate with others using Git.
Cooperate with others
Git is a primary tool for cooperation between developers. If you want to contribute to an open-source project, you will most likely do this with the help of Git and a repository hosting service like GitHub. If you’re working on a project at your company, there’s also a high chance that you and your colleagues cooperate using Git and slightly different hosting services like GitLab or Bitbucket. So, let’s take a look at these hosting services and see how they integrate with Git.
Hosting services and Git
If you’re just starting your journey in tech and learning about Git, it can be confusing when people use the words GitHub or GitLab interchangeably with Git. Let’s see what the difference is between hosting services and Git software.
Hosting services like GitHub can host your remote Git repository. It’s a repository available to everyone online (unless you mark it as private). This service is not Git software itself; you’re free to use Git without any hosting service at all.
However, GitHub and similar services use Git software to give you access to your project online– both through Git command and their websites. Why is that important? With your repository available online, you can access it from any computer you like, and others can contribute to your project or use it.
Those hosting providers also introduced an exceptionally helpful feature : pull requests (or merge request in GitLab). A pull request is used when you want to merge your changes from a feature branch into the main branch (or any other branch). However, before doing so, you can display the changes you intend to make and receive feedback on them, which significantly improves the quality of your code. Additionally, GitHub is a great place to showcase your skill in practice.
While other tools with similar features exist, such as Mercurial, mastering Git is sufficient.Even if people at your companyuse Mercurial, you’ll be able to catch up quickly.
I hope this article clarified some aspects of Git–in the end, it’s just a tool that helps us organize our projects. Of course, there are tools that can do the same, but at this moment, we can say that Git has dominated the industry–for good reason. If you know Git, you’llunderstand other similar tools. If you want to dive deeper into how a typical workflow in Git works, check out this article.