Git Essentials: A Comprehensive Guide to Version Control

Git Essentials: A Comprehensive Guide to Version Control

What exactly is 'version control', and why is it significant? Version control is a systematic method of keeping track of changes made to a file or a group of files. It automatically documents changes over time for you, also enabling you to retrieve specific versions of your project later on. Popularly used to manage source code, it's important to note that this process can be applied to almost any type of file on a computer.

For people who wish to preserve every version of an image or layout (which is highly recommended), adopting a Version Control System (VCS) proves to be a perfect choice. A VCS facilitates the ability to roll back selected files to a previous state, restore the entire project to an earlier version, compare modifications over time, identify the last contributor to a particular file causing issues, pinpoint the introduction of a problem and when it occurred, and more. Opting for a VCS also generally ensures an easy recovery in case of mistakes or file loss. Moreover, these benefits are low-cost and efficient.


What is Git?

Git is officially defined as a distributed version control system (VCS). Git stands out as a hugely popular tool, facilitating seamless collaboration on projects for both, individuals and teams. It's excellent for keeping things organized and ensuring everyone on the team can contribute. Needless to say, knowing how to use Git is an important skill for any developer.

Installation

Git is run on the command line, to use it we first need to install Git bash. You can find the download page here: https://git-scm.com/downloads.

On Windows, after the installation is complete, you can open Git Bash from the Start menu to use Git from the command line. For MacOS, If you have Homebrew installed, you may open your terminal and run:

brew install git

After installation, start your terminal and type the following command to verify that Git is ready to be used on your computer:

git --version

This command should display the installed Git version, confirming that Git is now available on your system. Now, you can configure Git with your name and email using the following commands:

git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"

Replace the values inside the quotes with your name and email address. This is simply like your unique username and is used to record the changes made in tracked files by a user.

What is a repository?

When working with a VCS like Git, it's important to be familiar with the term repository. A Repository(often abbreviated as "repo") is a container for a project that is tracked by Git.

We can classify them into 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 - The project/files are generally stored 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.

To track your project/files with Git by creating a new repository, use your terminal and navigate to the main folder of your project, then type the following command:

git init

This should make a hidden .git directory for your project, where Git can store all internal tracking data for this repository.

Commits

Everytime someone wants to save any changes in the source code, it is saved as a "commit". A commit is a record of changes to a repository. It represents a snapshot of the project at a specific point in time. Commits are accompanied by commit messages(like comments) that describe the changes made. 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.

To check the status of a current repository we can use the command:

git status

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.

Whenever a user is ready to commit new files, he places his files in a staging area. From here, the changes are saved into the repository using the merging command (discussed ahead). The status command helps us keep track of what files are added to the staging area too.

Staging area and committing

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.

To add a particular file, use the following command:

git add script.js

To add multiple files, use this:

git add index.html styles.css script.js

To add all of the files in your folder at once, write:

git add .

To commit the files from the staging area, use this command:

git commit -m "Commit message"

The commit message in the inverted commas is like a comment, for you to give a brief description about your commit and files changed.

To create a new commit, you will need to repeat the process of adding files to the staging area and then committing them after. Therefore, the git status command is quite useful to see which files were modified, staged, or untracked.

If you want to see all the commits that were made in a repo, use the following command:

git log

This will show details for each commit: the author name, a generated hash for the commit, date and time of the commit, and the commit message if provided. To go back to a previous state of your project code that you committed, you can use the following command:

git checkout <commit-hash>

Replace <commit-hash> with the actual hash for the specific commit that you want to visit. To go back to the latest commit, type:

git checkout master

If you wish some files to not be tracked at all, you can add them to the .gitignore folder. Read more.

Note: in recent times, many repositories have started using "main" as the base branch instead of "master". keep that in mind while jumping between branches.

Branches and Merging files

A branch is like your independent copy within a repository to work on. It allows you to work on features or bug fixes without affecting the main codebase. Branches can then be merged back into the main branch(which has the main codebase) when the changes are ready. Merging is the process of combining changes from one branch into another.

To create a new branch, use the following command:

git branch <new-branch-name>

This new branch will be a clone of your current state of the repository. It is good practice to make a new branch before building new features, experimenting with some new ideas, etc. After making sure all test cases are bug-free and features are ready to use, you can merge them into the main branch.

To switch to a different branch, you use this command:

git checkout <branch-name>

To create a new branch and switch to it at the same time, use the -b flag:

git checkout -b <new-branch-name>

To merge the changes from a different branch into your current branch, use this command:

git merge <branch-name>

Replace <branch-name> with the branch that you want to integrate into your current branch.

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

git branch -d <branch-name>

Some helpful resources:

  1. Official documentation: Online book

  2. Cheat sheet: git-sheet

Thank you :)