Introduction to Git In 16 Minutes

Introduction to Git In 16 Minutes

The Beginner’s guide to Git - Learn Git in 16 Minutes

This article is going to cover the basics on everything there is to know about Git. A handy developer tool, that happens to be a bit difficult to grasp. There's a lot to cover, so let's get into it!


What is Git?

Git is a free, open-source, distributed version control system (VCS). It's a system that tracks changes to our project files over time. It enables us to record project changes and go back to a specific version of the tracked files at any given point in time.

This system can be used by many people to efficiently work together and collaborate on team projects, where each developer can have their version of the project distributed on their computer. Later, these individual versions of the project can be merged and adapted into the main version of the project.

It's ubiquitously popular for coordinating parallel work and managing projects among individuals and teams. Knowing how to use Git is an essential skill for any developer nowadays - and it's a great addition to your resume!

1. Setup instructions

Okay, so the first thing you need to do is to have Git installed on our machines.

You can download Git here:

Click the download link for your specific operating system, and then follow through the installation wizard to get things set up on your computer!

Yeah! Now that you have Git installed, open up a terminal, and type the following command to verify that Git is ready to be used on your computer:

git --version

If everything goes well, it should return the version of Git installed on your computer.

🖥 If you are using a Mac or Linux machine, then you can utilize the default Bash terminal that comes pre-installed on your machine.

🖥 If you are using Windows, you can use its built-in Powershell terminal, or the Git Bash terminal which is bundled with the Git installation

Now let's Configure Git With Your Name & Email

In your terminal, run the following commands to identify yourself with Git: bash git config --global "Your Name" git config --global "" Replace the values inside the quotes with your desired username and email address.

Note: This information is not used to log in anywhere, This is only used to track who made what changes, So that username and email is just used to identify yourself

2. Repositories

When working with Git, it's essential to be familiar with the term repository. A Git repository is a container for a project that Git tracks.

We can single out two major types of Git repositories:

  • Local repository - an isolated repository stored on your machine where you can work on the local version of your project.

  • Remote repository - generally stored outside of your local machine, usually on a remote server. It's especially useful when working in teams - you can share your project code, see other people's code and integrate it into your local version of the project, and push your changes to the remote repository.

In this article, we'll only talk about local repositories.

3. Initializing a repository

Now that we know about repositories let's create one. To create a new repository and start tracking your project with Git, use your terminal software and navigate to the main folder of your project, then type the following command:

git init

This command will generate a hidden .git directory for your project. It is where Git stores all internal tracking data for the current repository.

3.1. Checking the status

Now that we have initialized our repository, let's talk about git status . While located inside the project folder in our terminal, we can type the following command to check the status of our repository:

git status

This is how you check the current status of your repository. This is a command that is very often used when working with Git. It shows us which files have been changed, which files are tracked, etc.

4. Staging and committing code

Committing is the process in which the changes are ' officially' added to the Git repository.

In Git, we can consider commits to be checkpoints or snapshots of your project in its current state. In other words, we save the current version of our code in a commit. We can create as many commits as we need in the commit history, and we can go back and forth between commits to see the different revisions of our project code. That allows us to efficiently manage our progress and track the project as it gets developed.

Commits are usually created at logical points as we develop our project, usually after adding specific content, features, or modifications (like new functionalities or bug fixes).

4.1. Staging files

We can use the git add command from the project folder to add our files to the staging area, which allows them to be tracked.

We can add a specific file to the staging area with the following command:

git add file.js

To add multiple files, we can do this:

git add file.js file2.js file3.js

Instead of having to add the files individually, we can also add all the files inside the project folder to the staging area using the command:

git add .

By default, this adds all the files and folders inside the project folder to the staging area, from where they are ready to be committed and tracked.

4.2. Making commits

A commit is a snapshot of our code at a particular time, which we are saving to the commit history of our repository. After adding all the files that we want to track to the staging area with the git add command, we are ready to make a commit.

To commit the files from the staging area, we use the following command:

git commit -m "Commit message"

Inside the quotes, we should write a commit message which is used to identify it in the commit history.

The commit message should be a descriptive summary of the changes that you are committing to the repository.

After executing that command, you will get the technical details about the commit printed in the terminal. And that's it. You have successfully made a commit to your project! 🎉🎉 .

Hurray, we have just successfully made our first commit! , now if we type, git status we should see:

download (4).jpg

Before we can commit any changes in our code, we need to decide which files or which changes we to place (add) in inside the staging area. and from the staging area we commit

4.3. Commit history

To see all the commits that were made for our project, we use the following command:

git log

The command git log shows the details for each commit, like the author name, the generated hash for the commit, the date and time of the commit, and the commit message that we provided.

Now let's say we made some changes to our code ( like added new functionalities or bug fixes e.t.c ), and for some reason, our code breaks and stops functioning like it's supposed to 😔 .

That's where Git comes in handy and why developers love it so much!.

With Git, we can easily revert to a safe version of your project where our code was working

git checkout <commit-hash>

Now Replace <commit-hash> with the actual hash for the specific commit that you want to visit, which is listed with the git log command.

To go back to the latest commit (the newest version of our project code), you can type this command:

git checkout master

4.4. Ignoring files

To ignore files that you don't want to be tracked or added to the staging area, you can create a file called .gitignore in your main project folder.

Inside that file, you can list all the file and folder names that you definitely do not want to track (each ignored file and folder should go to a new line inside the .gitignore file).

You can read an article about ignoring files on this link.

5. Branches

A branch could be interpreted as an individual timeline of our project commits.

Think of it like in the flash when berry goes to different alternate timelines of his life ( past, present, and future!), all occurring separately at the same time 😉

With Git, we can create many of these alternative environments (i.e., we can create different branches) so that other versions of our project code can exist and be tracked in parallel.

This feature allows us to add new (experimental, unfinished, and potentially buggy) features in separate branches without touching the main 'official' stable version of our project code (which is usually kept on the master branch).

When we initialize a repository and start making commits, they are saved to the master branch by default.

5.1. Creating a new branch

You can create a new branch using the following command:

git branch <new-branch-name>

The new branch that gets created will be the reference to the current state of your repository.

It's a good practice to create a development branch where you can work on improving your code, adding new experimental features, and similar. After developing and testing these new features to make sure they don't have any bugs and that they can be used, you can merge them to the master branch.

5.2. Changing branches

To switch to a different branch, you use the command, git switch:

git switch <branch-name>

With that, you can switch to a different isolated timeline of your project by changing branches.

For example, you could be working on different features in your code and have a separate branch for each feature. When you switch to a branch, you can commit code changes that only affect that particular branch. Then, you can switch to another branch to work on a different feature, which won't be affected by the changes and commits made from the previous branch.

If the branch we want to switch to doesn't exist then the git switch <branch-name> command will create a new branch and change to it at the same time

git switch  <new-branch-name>

To list all the branches for your project, use the command: git branch

To go back to the master branch, use this command:

git checkout master

5.3. Merging branches

Now, this is the last piece of our puzzle.

So let's say you have created a new branch separate from the master branch of your project that you want to work on. after you've fully implemented and tested a new feature in your code, you would want to merge those changes to the stable branch of your project (which is usually the default master branch).

How do we do that? It's easy. To merge the changes from a different branch into your current branch, you can use this command:

git merge <branch-name>

You would replace <branch-name> with the name of the branch you want to integrate into your current branch.

5.4. Deleting a branch

To delete a branch, you can run the git branch command with the -d flag:

git branch -d <branch-name>

Read more about branching and merging on this link.

Wrapping Up

Now you know the basics of Git and how it works. There's a ton more to Git that we haven't talked about. To learn more about Git, make sure to check the following resources 👇:

Give these resources a try, and ultimately, you'd get to fully comprehend how GIt works.

** As always, thanks for giving it a read, give it a like 👍, share it with others too, and if you still got any questions, then drop them down in the comments. THANKS FOR READING! ** 💖

if you enjoyed reading this as much as I enjoyed writing it, then Like and Share this with your friends and feel free to follow me on Twitter 👨‍💻.

Buy Me A Coffee