DEV Community

Cover image for Introduction to Version Control with Git.
Brandon Damue
Brandon Damue

Posted on • Edited on

Introduction to Version Control with Git.

When I started out as a web development student early last year, one of the tools that was tricky for me to understand from the get-go was version control, so I avoided the use of Git(a version control tool) altogether. I must say this was a very wrong choice I made as version control is one of the most important things to know and use as a web developer. I have come to discover that many beginners in web development are like me when I started out, they avoid using Git or any other version control.

This article a beginner friendly introduction to Git, a distributed version control system. I am going to talk on what git is all about and some basic Git commands to get you started. I will not go over the step-by-step installation process for Git for two reasons which are these steps might change overtime and going over it will make this article unnecessarily long. So without further ado let's get started.

What is Git?

Before we begin, it is important that we have a brief overview of what Git is. It is a distributed version control system for tracking changes in source code during software development so that any version of these files can be retrieved at will. I'm sure you are probably wandering what I mean by the terms "distributed version control system", let's break that down. "Distributed" means when files are shared with Git, all the versions of the file can be accessed by any party with whom the files are shared. "Version Control System" in a lay man's language is simply a tool that helps us keep track of our files and the different versions of those files for future reference.

File States in Git

A file tracked by Git can be in one of three file states at any point in time. These states are; the modified state, staged state and committed state. It is okay if you don't understand the meaning of those words yet, Git does have its own vocabulary.

  1. #### Modified State. Files in this state are files that have been modified but git has not yet been instructed to monitor this recent modification.
  2. #### Staged State Files in this state are the files that have been selected in their current state (version) and are being prepared to be saved (committed) into the .git repository in the next commit snapshot. Files in this state are on a conceptual yellow brick road to go to the .git repository.
  3. #### Committed State Files in the committed state are files successfully stored into the .git repository. So a committed file is a file in which you have recorded its staged version into the Git folder or directory.

Git Commands

Though working with Git on the command line can be a bit daunting, you only need a few of git commands for basic git operations. As with many tools in a developer's work life, the use of these commands become easier with practice. If you are one of those beginners who dread using the command line, Git has many clients that allows you to use Git without the command line. But it is still important to know the commands that are executed by the client behind the scenes. I'll show you some of these simple Git commands , what they do and how they are used. We are going to do this practically for better understanding, so just create a folder and name it git_tutorial, create a file named git_tutorial.txt. Open your command prompt or terminal and change directory into our git_tutorial folder . We can now begin.

git init

This command creates an empty git repository in the current directory.
This is the first step in creating a repository. After running git init, adding and committing files/directories is made possible.

In Practice

cd into the git_tutorial direct and run the following command to initialise(create) a git repo.

git init
Enter fullscreen mode Exit fullscreen mode

Screenshot 2021-02-23 at 13.25.49.png

If an empty Git repo was created successfully, you see a Initialise empty Git repository in <path/to/directory/.git/> output in your terminal.

git add

This command adds a change in the working directory to the staging area(index). It tells Git that you want to include changes or updates to a particular file or files in the next commit. . There are a few different ways to use git add, by adding entire directories, specific files, or all unstaged files( files in the working directory).

git add <name-of-file>
Enter fullscreen mode Exit fullscreen mode

In Practice

Now let's add our add our git_tutorial.txt to the staging area by running the the git add git_tutorial.txt.

Screenshot 2021-02-24 at 04.39.01.png

As you must have noticed when you ran the git add command, no output is returned to the terminal, this is because git add doesn't really affect the repository in any significant way—changes are not recorded until you run the git commit command.

git status

This command is used to show the state of the working directory and the staging area. It lets you see which changes have been staged, which haven’t, and which files aren’t being tracked by Git. It is good practice to always run git status every now and then to see the state of the working directory and staging area.

Note that the git status output does not show you any information regarding the committed project history. If you want to display the commit history of a project, use git log.

git commit

To commit means to save changes in the staging area locally. This command records a snapshot of files that are currently in the staging area or index waiting to be committed. Git tries to keep things as lightweight as possible by not blindly copying the entire repository every time you commit it only saves changes that have been made.

In Practice

To commit changes run the git commit -m "<commit-message>" here refers to a description of the changes this commit is saving. Most developers use "Initial commit" as the commit message for their first commit.

Screenshot 2021-03-02 at 14.07.52.png

Now if you run the git log command you are going to see your commit history. As of now our changes are now saved locally on our computer. You can always put your local repo on a remote server such as GitHub or GitLab using other commands which I haven't talked about in this article.

Conclusion

They are many more git commands that are used by developers but these ones will be able to get you started with version control using Git. Keep in mind that you don't have to memories these commands, try as much as you kind to understand what they do. Make use of Google in case you get stuck on something or you have a problem using any of the commands.

ps: I just figured out that I won't be doing your justice if don't share some other useful resources that will take you from "nothing to something" in Git. Check out Udacity's Git course here. This course was very useful to me when I started learning version control with Git.

Top comments (2)

Collapse
 
brandondamue profile image
Brandon Damue

It is critical indeed Mr Peter. I just hope some web developers especially those who are new to the field can stop underestimating the power of Git and start using it to keep track of their projects. It is such a great and powerful tool.

Collapse
 
ptrjsn profile image
Peter Johnson • Edited

Good article! I'm glad you got into version control early; it's so critical. I've been working with version control for over 20 years & have recently been working on my Git skills, comparing workflows, etc. The aspects you covered are common across many VCS's; local vs. remote, branching, & merging are where Git differs, & can be some of the harder concepts to grasp. But as with most things, you can start simple, & learn the complexities once you get that down.