DEV Community

Juan Cruz Martinez
Juan Cruz Martinez

Posted on • Originally published at livecodestream.dev on

What is Git? The ultimate introductory guide to Git

Live Code Stream

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.


What Is a Version Control System?

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!


What Is Git, and Why Is It Used?

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.


Benefits of Using Git

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.


Who Uses Git?

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.


Installing Git

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.

Install Git on MacOS

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:

git --version

Enter fullscreen mode Exit fullscreen mode

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:

xcode-select --install

Enter fullscreen mode Exit fullscreen mode

For Homebrew:

brew install git

Enter fullscreen mode Exit fullscreen mode

Install Git on Linux

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

Enter fullscreen mode Exit fullscreen mode

For Arch-based distros:

pacman -S git

Enter fullscreen mode Exit fullscreen mode

Install Git on Windows

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.


Setting Up Your Git User

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>"

Enter fullscreen mode Exit fullscreen mode

Basic Concepts of Git

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.

Initializing a Git repository

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:

git init 

Enter fullscreen mode Exit fullscreen mode

Note that at this point, all changes are only tracked locally. No server is required.

Cloning a remote repository

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> 

Enter fullscreen mode Exit fullscreen mode

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.

Pulling new changes from the remote repository

Git pull
Git pull

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 pull 

Enter fullscreen mode Exit fullscreen mode

Committing changes

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"

Enter fullscreen mode Exit fullscreen mode

Git development environment sections
Git development environment sections

With the 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.

The 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 git push.

Pushing changes to a remote repository

Git push
Git push

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>

Enter fullscreen mode Exit fullscreen mode

The 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.

Find out what has changed since the last commit

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.

git diff 

Enter fullscreen mode Exit fullscreen mode

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.

Stashing changes

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:

git stash 

Enter fullscreen mode Exit fullscreen mode

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.


Working with Branches

Git branching model
Git branching model

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 main or 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

Enter fullscreen mode Exit fullscreen mode

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>

Enter fullscreen mode Exit fullscreen mode

Finally, to switch from one branch to another, you can run the following:

git checkout <branch_name>

Enter fullscreen mode Exit fullscreen mode

Git merge

Git merge
Git merge

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>

Enter fullscreen mode Exit fullscreen mode

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.

Pull Requests

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.


Advanced Features of Git

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.


FAQs

What is the difference between GitHub and Git?

As we discussed, Git is a version control system used to track changes in source code over time. GitHub, on the other hand, is a repository hosting service for Git, where developers can store and collaborate on projects. It also has many additional features that make software development easier, like project management tools and web-based code editors.

Is Git a programming language?

No, Git is not a programming language; it is, however, the glory of version control systems.

Is Git easy to learn?

Yes, Git is easy to learn, but like with any of these advanced systems, you can pick up most of what you need for your daily work in less than a week, but if you want to be an expert on Git and the command line, that would require some additional time, practice and experience.

Most developers understand the concepts behind Git and have minimum knowledge of the CLI and depend on UI tools for most of the tasks.

Where can I learn Git?

In this guide, we provide you with the basics you need, but if you prefer a more visual approach, there are a few online courses you can take to learn more about this awesome tool.

(Some links may be affiliate links. This means if you click on the link and purchase an item we may receive commission at no extra cost to you.)

What's the difference between a distributed version control system and a version control system?

A distributed version control system (DVCS) works similarly to a version control system in saving changes made to files over time. The main difference is that DVCSs allows multiple users to work on the same project simultaneously without requiring a central server. This provides improved scalability, reliability, and security compared to the traditional version.

Git is one of the most popular version control systems and has become essential to developers worldwide. With its impressive scalability and reliability, it’s no wonder why so many have adopted this powerful tool.

Who created Git?

Git was originally created by Linus Torvalds, the Linux kernel creator. Git was initially created to serve the Linux kernel development, and in 2005, it turned into an open-source version control system.

Conclusion

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.

Happy coding!

Subscribe to my weekly newsletter for developers and builders and get a weekly email with relevant content.

Top comments (0)