Git is an essential tool for software developers and other professionals to store source code and project files while keeping track of the project’s complete history and allowing multiple people to collaborate on the same files without worrying about conflicts.
In this article, we’ll explore what Git is, what it can do, and how to get started using it. We’ll also discuss why Git has become such an essential part of modern development processes and what makes it so powerful.
So let’s dive in.
A version control system, or source control system, is like a library where you can store different versions of your work. You use it to track all the changes made to something and which version is the most up-to-date. It’s like a big family tree that shows who did what and when!
Git is an open-source version control system that tracks code file changes over time. It allows developers to track what has changed, when, and by whom. To power that, Git also helps developers work in teams on a project by handling conflict resolution and merging different versions of the same codebase.
By nature, Git is a distributed version control system, meaning every user has a local copy of the entire repository (source code and project history). That makes it fast and allows for much flexibility in how different people work on the same code base.
Git is a powerful tool that can help any developer work more efficiently and collaboratively. Here are some of the key benefits:
Easy tracking of changes: Git makes it easy to track what has changed in the codebase, when, and by whom. This allows teams to quickly identify what needs to be done and who is responsible for what.
Version control: Git makes it easy to keep multiple versions of the same codebase so teams can experiment without fear of messing up existing work.
Flexibility and scalability: Git is distributed, which makes it more flexible than many other version control systems. It also allows for scaling as team size increases.
Collaboration: Git enables developers to collaborate more efficiently, allowing them to easily share their changes and quickly merge each other’s work.
Git is the most popular version control system by far. According to the 2022 Stack Overflow survey, almost 94% of the respondents claimed to use Git. From big companies like Google, Meta, Netflix, etc., to mid-size companies and startups, they all use Git in one way or another.
It is also primarily known for its use with open source projects. Most developers would use Git to store their portfolios or personal projects, all thanks to services like GitHub that provide free Git for open projects.
Now that you know what Git is and what it can do, you may be wondering how to get started. Fortunately, getting started with Git is relatively easy, even for beginners.
Many resources are available online that offer tutorials and helpful tips on using Git. Additionally, various tools, such as GUI clients and command-line interfaces, can make it easier to use Git.
So what are you waiting for? Give it a try and see how powerful and efficient Git is! You won’t regret it!
Next, we will cover how to install Git on each popular operating system.
If you’re a Mac user like myself, I recommend two options to install Git. First, the one I use is using XCode tools, and the second option is using Homebrew. The main difference between the two is the Git version, Homebrew would generally have a more up-to-date Git, but both are great options.
Before installing Git, make sure it’s not installed yet by running the following command on your terminal:
If Git is not installed, running any Git command may prompt you directly to install Git using the XCode tools option. If that’s the case, accept, and you’re done.
If you want to install Git using XCode tools, run the following:
brew install git
You can install Git by simply installing a package using your favorite package manager.
If you are on a Debian-based distro (like Mint, Ubuntu, etc.), you can install Git by running the following command:
sudo apt-get install git
For Arch-based distros:
pacman -S git
The best way to install Git for windows is to use the GUI installer. You can then follow the instructions on the wizard and get Git up and running in no time.
Once you’ve installed Git, it’s time to configure it. You will need to set up your username and email address so that Git can properly identify who committed what changes.
To do this, run the following two commands:
git config --global user.name "<your-name>" git config --global user.email "<your-email-address>"
Now that you have Git installed and configured, let’s take a look at what it does and how it works.
There are two main ways to set up a local Git repository, by initializing a new repository or by cloning an existing repository from a remote source.
If you want to start tracking changes in a new project or directory, you can initialize a local Git repository.
This will create the necessary files and folders for the repository so that you can begin committing your changes.
To do this, run the following command:
Note that at this point, all changes are only tracked locally. No server is required.
If you want to work on an existing project hosted on a remote Git instance, you can simply
clone the project to a local folder. When you clone a remote repository, the full source code and the entire commit history of the project will be downloaded.
To clone a repository, run the following command:
git clone <URL>
Once you have a local copy of the remote repository, you can work on it and commit any changes locally. You can then push your changes back to the remote server.
If you have already cloned a remote repository, you can easily pull any new changes from the remote server. This will download all the commits that were made since your last pull and merge them into your local copy of the repository.
To do this, run the following command:
Git doesn’t auto-save any changes into its history, and for good reasons, you don’t want to track every single word you add to a file, or its history would rapidly become overwhelming to analyze. Instead, Git relies on the concept of “commit”, which saves all the local changes you want to save into the Git repository’s history.
Because we can group changes across files together, and we also have options to enter a message to each commit, we can make sure that our commit history is logical and easy to understand.
To commit a change, run the following command:
git add . git commit -m "My first commit"
git add command, you can incorporate modifications to a file in your working directory into the staging area. Basically, it notifies Git of any changes that need to be included in the following commit. Despite this, no major alteration will happen until you run
git commit; hence why,
git add is simply an intermediary step for adding potential updates into consideration. For
git add command, you also need to specify which files you want to add or use the symbol
. to add all modified files.
git commit command is what actually records the changes you made. With this command, you will also add a description of what was done with the addition of
-m followed by a message. This lets other contributors know what kind of modifications were included in this commit.
It is important to note that even when you commit changes, those changes are tracked locally, and nothing is altered on the remote (or sever) until you run
Once you’ve committed your changes to a local repository, you can push them up to a remote repository. This is what allows collaborators to work on the same project and keep up-to-date with each other’s progress.
To push your changes, run the following command:
git push <remote-name> <branch-name>
remote-name is usually set to the name of the repository you cloned from. If you use the git clone command, this will be correctly set automatically. The branch name refers to what branch of the remote repository you want to push your changes to.
You can use
git diff to check what has changed since the last commit. This command compares your current file version with what is stored in the repository, allowing you to see what has been modified and hasn’t.
The output of
git diff will display any additions or deletions since your last commit. If a file changes slightly, it will also show what was added/deleted on each line.
Git also has the ability to
stash changes. This allows you to save your current working state without committing it. It can be used if you need to switch branches quickly or if you need to switch the focus of your work but are not quite ready to commit yet.
Many new developers don’t know about this useful feature. They would, instead, commit incomplete work, which can be cumbersome in large projects or projects where the Git history should be as clean as possible.
Stashing changes is done using the following command:
This command will save the current changes on your working directory and reset it to the last committed. To restore the changes, you can use
git stash pop, or
git stash apply if you want to keep the stashed change.
Branching is what makes managing code versioning more powerful. As opposed to a linear approach, like what we have been discussing so far, Git branches allow developers to create new “versions” of their codebase and track changes independently.
For example, let’s say you are working on a feature branch called
my-feature. This branch will be used to track all the changes that are related to that particular feature. This way, you can keep your main codebase (usually referred to as
master) separate from what’s being done in the feature branch.
Creating a new branch is easy; just type this command:
git checkout -b my-feature
This will create a new branch called “my-feature” and switch to it. Once you’ve made all the changes that you need, you can simply commit them and then switch back to the main branch using
git checkout master.
From here, you can either merge your feature branch into the main branch with
git merge my-feature, or you can create a Pull Request, which is what we’ll be discussing next.
If a feature is drop, or you don’t need your branch anymore, you can delete a branch by running:
git branch -D <branch_name>
Finally, to switch from one branch to another, you can run the following:
git checkout <branch_name>
Let’s suppose you have made some changes to a new branch and want to integrate them with your master branch. You can do that by performing a merge operation.
git merge <branch_name>
In simple words, this command is used to combine two branches. If a specific file is changed in both branches, then it will create a merge conflict. In that case, you have to manually look into the file and select the change you want and remove the other one.
Though merging can be done through the command line, it is more commonly done with the use of pull requests (PR), and visual interfaces.
A pull request (PR) is what developers use to suggest changes to a project’s codebase. It allows other contributors to review the proposed changes and either accept them or provide feedback on how to modify them.
Git services like GitHub and GitLab make this process easier, providing a web-based interface for creating, discussing, and merging PRs.
So far, we have covered the most essential features of Git, which you will use in your everyday life as a developer. But there is a ton more you can do with Git. Since advanced features are a bit out of the scope of this guide, I created two more articles you can check out to learn more about advanced Git features.
- Git concepts that make you look like a pro - part 1
- Git concepts that make you look like a pro - part 2
Git is an essential tool for any developer, and mastering it can go a long way. In this post, we went over what Git is and what it does, how to set it up on your machine, and how to use the most common git commands. We also discussed some more advanced features of Git, like stashing changes and working with branches.
I hope this guide helped you get a better understanding of what Git is and how to use it. Finally, make sure to check out the additional articles I linked in this post if you want to learn more about advanced Git concepts.