DEV Community

Cover image for An Introduction to Version Control and Git
Erika Heidi
Erika Heidi

Posted on

An Introduction to Version Control and Git

Git is a powerful version control system that allows developers to track changes to code, collaborate with others, and manage multiple versions of a project. By mastering Git in the command line, developers gain the flexibility to perform various tasks efficiently, such as creating and switching between branches, resolving merge conflicts, and managing remote repositories. Additionally, familiarity with the command line provides a deeper understanding of Git's inner workings, enabling developers to troubleshoot issues and customize their workflows more effectively.

In this guide, beginners can learn about version control and how to get started with Git. To follow along, you’ll need access to a terminal and administrative privileges to install Git.

Overview

In a nutshell, version control is a system that allows you to track changes to files over time. This is useful for a variety of reasons, including:

  • Collaboration: Version control allows multiple people to work on the same files at the same time without overwriting each other's changes.
  • History: Version control keeps a history of all changes made to files, so you can see who made a change, when they made it, and why.
  • Rollback: Version control allows you to roll back to a previous version of a file if you make a mistake.

Git is a popular version control tool widely used for software development, but it can also be used for any type of project that involves managing multiple files. Git is designed to be efficient, flexible, and easy to use. It is a powerful tool that can help you to manage your projects more effectively.

Installing and Configuring Git

The official Git documentation has detailed instructions on how to install Git on all supported systems. On Ubuntu, you could run the following to get Git installed:

sudo apt update && sudo apt install git
Enter fullscreen mode Exit fullscreen mode

To verify that the installation was successful, you can run:

git --version
Enter fullscreen mode Exit fullscreen mode

Once you have Git installed, you’ll need to configure it with your name and email:

# Set your Git name
git config --global user.name "Your Name"

# Set your Git email
git config --global user.email "your@email.com"

# Configures default initial branch
git config --global init.defaultBranch "main"
Enter fullscreen mode Exit fullscreen mode

Now you can start using Git!

The Basic Git Workflow

The basic Git workflow involves a cycle of making changes to files, staging those changes, committing them to your local repository, and then pushing them to a remote repository. Staging changes allows you to group together related changes before committing them. Committing changes creates a snapshot of your project at a specific point in time, along with a message describing the changes. Pushing changes to a remote repository makes them available to other collaborators and backs up your work in case something happens to your local repository. This cycle allows you to track changes, collaborate with others, and easily revert to previous versions of your project.

The image shows an analogy for the git workflow using letters. When you're working on the contents of your letter, you have unstaged changes. When you're ready, you'll add the letter to an envelope, and that's the equivalent of adding files to your staging area. Finally, you'll commit your changes by closing the letter and adding a stamp  on it. After all that, you still need to push your changes to the remote repository, which is the equivalent of pushing your letter into a mailbox.

The image shows an analogy for the git workflow using letters. When you're working on the contents of your letter, you have unstaged changes. When you're ready, you'll add the letter to an envelope, and that's the equivalent of adding files to your staging area. Finally, you'll commit your changes by closing the letter and adding a stamp on it. After all that, you still need to push your changes to the remote repository, which is the equivalent of pushing your letter into a mailbox.

Creating your First Git Repository

We’ll now cover some basic Git commands to get you started and demonstrate the Git workflow.

Create a test directory and cd into it:

mkdir testgit && cd testgit
Enter fullscreen mode Exit fullscreen mode

Next, initiate an empy git repository:

git init
Enter fullscreen mode Exit fullscreen mode

You should get output similar to this:

Initialized empty Git repository in /home/erika/testgit/.git/
Enter fullscreen mode Exit fullscreen mode

All files in this directory are now being tracked by Git. Create a new file so that we can try a commit:

echo "Git Crash Course" > readme.txt
Enter fullscreen mode Exit fullscreen mode

Now, if you check the status of the repository, you’ll notice the new readme.txt file listed as untracked:

git status
Enter fullscreen mode Exit fullscreen mode

You’ll get output like this:

On branch main

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
    readme.txt

nothing added to commit but untracked files present (use "git add" to track)
Enter fullscreen mode Exit fullscreen mode

As the message suggests, you can add files to be later committed with the git add command:

git add readme.txt
Enter fullscreen mode Exit fullscreen mode

Now, if you run git status again, you’ll notice that the readme.txt file has moved to staged, which means it is now part of the changes that should be committed. The output also points out this is a new file, with no previous history to Git.

On branch main

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
    new file:   readme.txt
Enter fullscreen mode Exit fullscreen mode

To commit changes, you can use the following command, which commits staged files and uses an inline message to identify this commit:

git commit -m "first commit"
Enter fullscreen mode Exit fullscreen mode

You’ll get output similar to this:

[main (root-commit) 8e822c2] first commit
 1 file changed, 1 insertion(+)
 create mode 100644 readme.txt
Enter fullscreen mode Exit fullscreen mode

Hooray 🎉 You just created your first commit. You can check all recent changes in a repository with the git log command:

git log
Enter fullscreen mode Exit fullscreen mode

You should get output similar to this, showing your first commit to the repo:

commit 8e822c2c3b8b2872da87c6716f426282bb8a6340 (HEAD -> main)
Author: Erika Heidi <erika@erikaheidi.com>
Date:   Thu Nov 14 19:30:35 2024 +0100

    first commit
(END)
Enter fullscreen mode Exit fullscreen mode

To exit the log view, press q.

It’s worth noting that all these changes are only being tracked locally. There is no remote repository linked, so everything is running on your local machine. If you delete the testgit folder, nothing will be saved. In a more real world scenario, you would be running an additional command to push your commit to a remote repository. You'll learn how to do that in a later part of this series.

Conclusion

In this article, you learned the basics about Git, including how to initialize an empty Git repository, how to stage changes, and how to commit them. In the next part of this series, you'll create a new GitHub account and customize your profile with a profile README.

Top comments (1)

Collapse
 
kerkg profile image
kerkg

Cool, tho installation only works with linux and most beginners use Windows