A Complete Beginners Guide to Git

exwhyzed

ExWhyZee

Posted on March 23, 2021

A Complete Beginners Guide to Git

Hello Dev fam!
Today is the day we cover a tool, which is compatible with all the Operating Systems that are being used these days, allows users from all over the world to perform operations on a project remotely, is lightweight, secure, reliable (the list goes on), but, is perceived to be a bit difficult to grasp. Yes, we are talking about Git

Before we get things started, please save this post for future reference, and do follow me for more such content.

Let's Go!

image

Q) What in the world 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 own version of the project, distributed on their computer. Later on, these individual versions of the project can be merged and adapted into the main version of the project.

Basically, it's a massively popular tool for coordinating parallel work and managing projects among individuals and teams. Needless to say, knowing how to use Git is one of the most important skills for any developer nowadays - and it's definitely a great addition to your resume!

Q) How do I setup Git?

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

Voila! , you have Git installed.
Now, it's time to tart your terminal. Type the following command to verify that Git is ready to be used on your computer:

carbon

If everything went well during the process, it should return the Git version installed in your computer.

Note:
If you are a Mac or Linux user, hen you can utilize the default Bash terminal that comes pre-installed on your machine.

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

Next Step: Configure Git With Your Name & Email

In your terminal, run the following commands to identify yourself with Git, replace the values inside the quotes with your desired username and email address.

carbon (1)

Repositories

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

We can single out two major types of Git repositories:

Local repository - an isolated repository stored on your own computer, where you can work on the local version of your project.
Remote repository - generally stored outside of your isolated local system, usually on a remote server. It's especially useful when working in teams - this is the place where you can share your project code, see other people's code and integrate it into your local version of the project, and also push your changes to the remote repository.
In this article, we'll just be covering local repositories.

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:

1

This command will generate a hidden .git directory for your project created by Git , it is where all internal tracking data for the current repository is stored.

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:

g2

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

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 at its current state. In other words, we basically 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 in specific contents, features or modifications (like new functionalities or bug fixes).

Staging files

From the project folder, we can use the git add command 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:

carbon (3)

To add multiple files:

carbon (4)

Instead of having to add the files individually, we can add every required file inside the project folder to the staging area using command:

carbon (5)

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.

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:

carbon

We can write a commit message inside the quotes 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 basically it!, you have successfully made a commit in your project! 🎉 now if we type git status we should see this:

9HHIf327i

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

Commit history

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

carbon (1)

The git log command will show details for each commit, like, name of the author, the generated hash for the commit, date and time of the commit, and the commit message which we provided earlier.

Now, lets say we made some changes to our code ( for example, added new functionalities or bug fixes) and for some reason our code breaks and stop functioning like it's suppose to.

That's Where Git come in handy and why mighty developers across the globe love it so much!

With Git we can easily revert back to a safe version of your project where our code was working before by:

carbon (2)

Now Replace 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

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 the file, you can list name of every file & folder which you do not want to track (each ignored file and folder should go to a new line inside the .gitignore file).

To go deep into ignoring files, you can go through this GitHub article: https://docs.github.com/en/github/using-git/ignoring-files

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, but at the same

With Git, we can create many of these alternative environments (i.e. we can create different branches) so 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.

Creating a new branch

You can create a new branch using the following command:

carbon (3)

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 everything similar. After development 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.

Changing branches

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

carbon (4)

With this, 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 which 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 command will create a new branch and change to it at the same time

Merging branches

Here's the last part of the puzzle.

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

So 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:

carbon (5)

Just replace with the name of the branch that you want to integrate into your current branch.

Deleting a branch

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

carbon (6)

Alright, this article Ends here.

So that's it for this article , there's a ton more of Git we haven't talk about (we'll definitely do it sometimes later.)

Leaving you with some resources👇:

Git official documentation: https://git-scm.com/doc
The free Pro Git book: https://git-scm.com/book/en/v2
Learn about GitHub: https://guides.github.com/
eBook on how to get started with Git and GitHub (Suggested by user Bobby Iliev): https://github.com/bobbyiliev/introduction-to-git-and-github-ebook

Peace.

💖 💪 🙅 🚩
exwhyzed
ExWhyZee

Posted on March 23, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

What is GitHub?
github What is GitHub?

March 17, 2023

A Complete Beginners Guide to Git
beginners A Complete Beginners Guide to Git

March 23, 2021