
This is a three-part series on Git and GitHub. This is the first article in the series. This article starts our exploration of a version control system, Git, three states in Git, installing Git, and setting up your Git installation.
Version Control System
A version control system is a program that tracks changes to a file or a set of files in a certain folder. Whenever files change, these changes are recorded and stored. This system also allows you to revert selected files back to a previous state, or the entire project back to a previous state. So if you make mistakes in a code, you can easily go back to a previous state. There are also other features that let you see who made changes to what files and why.
What is Git?
Git is a version control system created by Linus Torvalds in 2005. When Git is initialized in a folder, it creates a hidden file that is used to track changes in that folder. This file stores snapshots of what the files look like at that moment. This folder is now termed a Git repository. A git repository is just an album of snapshots of your project.
When you modify any file in this folder and commit the changes, Git will take another snapshot and store it. These modifications may be adding or deleting codes, adding or removing a file and/or folder, moving files/folders, etc
But how does Git know which files have changed? Git normally creates a 40-character string composed of hexadecimal characters that are calculated based on the contents of a file or its directory structure to uniquely identify a file. This process is called ‘checksumming‘. So when you change a file, it will return a different hash string from the previous. That way, Git knows that the files or folder has changed.
Git is used to do three main things: track code changes, track who made the changes, and help developers collaborate. When Git is initialized in a folder, it creates a hidden file that is used to track changes in that folder. This file is used to store a snapshot of what the files look like at that moment.
Three States In Git
Files in Git can be in one of three states: modified, staged, and committed. These can be explained below:
A modified file is as the name suggests, modified but not yet committed to your repository. A file is considered modified if you edit the content inside the file. Adding and deleting a file is also a modification to a folder. You can make as many changes as you want to a file before staging it.
The second stage of a file is staged or the staging area. This means that you are satisfied with the changes you have made to the file and are ready to commit it to your repository. At this point, the file is not yet recorded in your repository. This stage is merely acknowledging that you are ready to add these changes to your next commit.
The last state is committed. At this stage, your data is safely stored in your repository. You can revert back to this state at any time in the future.
The basic Git workflow goes something like this:
- You modify files in the Git repository or working tree.
- You stage those changes you want to be part of your next commit.
- You do a commit, which invokes Git to take a snapshot of the files that have been staged.
Installing Git
Before you can start using Git, you have to install it. You can use Git either on the command line or using a graphical user interface (GUI). You can check out installation instructions at https://www.git-scm.com
After installing git, you can check the installation by running this command git –version
. That command will return a version number such as git version 2.XX.X
if Git is installed.
Setting Up Git
The first time you use Git, you will need to set a few things about your environment. These configurations will hold true across all repositories in your computer. Some of these such as username and email are needed before using Git.
Your Identity
The git config
command allows you to get and set configuration variables that control how Git operates. The first thing you should do when you install git is to set your name and email address.
git config –global user.name “John Doe”
git config –global user.email “johndoe@example.com”
This information is recorded with every change you make to a repository.
Your Default Branch Name
Git will create a branch called master when you create a new repository with ‘git init’
. This is the default name. To set a different name to the default branch, do: git config –global init.defaultBranch main
. After this command, any new repository in your computer will have main as the default branch name.
You can check your configuration settings by running git config –list
.
If you need help running Git commands, run git help <command>
or git <verb> --help
.