The Git Version Control System
Note to reader: This tutorial has a video on The Machine youtube channel which you can reference whenever you get stuck.
What is a version control system?
This is a system that allows you to track changes made to files over time, as well as collaborate with others on those files. Version control systems are commonly used in software development, but can also be used in managing changes to other types of files, such as documents, images, and videos.
Some examples of version control systems include Git — a popular distributed version control system, SVN (Subversion) — a centralized version control system, Mercurial — a distributed version control system, Perforce — a centralized version control system commonly used in the gaming industry to manage large codebases, and CVS or (Concurrent Versions System) — a centralized version control system that was popular in the 1990s and early 2000s.
Difference Between Distributed Version Control Systems (DVCS) and Centralized Version Control Systems (CVCS)
I am sure by now you are wondering what is the difference between a Distributed version control system (DVCS) and a centralized version control system (CVCS).
Let me explain:
Distributed version control systems (DVCS) and centralized version control systems (CVCS) are two different approaches to managing file changes to software code, documentation, or other digital assets.
In a centralized version control system, there is a single central repository that contains the latest version of the code. Developers check out files from the central repository, work on them locally, and then check them back in when they are done. This model has been the traditional approach to version control for many years, with tools like CVS, SVN, and Perforce being popular examples as mentioned earlier.
On the other hand, in a distributed version control system, each developer has their own local repository, which is a complete copy of the entire codebase. Developers can make changes locally and commit them to their local repository without any immediate connection to a central server. The changes can be shared with other developers by pushing them to a remote repository, which can be hosted on a central server or on a distributed platform like Git also mentioned before.
The key difference between these two approaches is the degree of centralization involved. In a centralized system, all changes flow through a central server, which can be a bottleneck or a single point of failure. In contrast, in a distributed system, each developer has their own copy of the codebase, making it more resilient and allowing for greater flexibility in managing development.
Overall, both centralized and distributed version control systems have their own benefits and drawbacks, and the choice between them will depend on the specific needs and preferences of the development team.
What Is Git?
Now let’s get to the most popular version control system in the world, used by 93% of developers according to StackOverflow. Git!
Git is a distributed version control system that allows users to track file changes, and collaborate with others on projects. It was created by Linus Torvalds in 2005 and has since become one of the most widely used version control systems in the world.
Git works by creating a local repository on your computer, where you can make changes to files and commit them to the repository. You can also create different branches of a project, where changes can be made and tested independently before being merged back into the main branch. This makes it easier to work on complex projects with multiple contributors, without having to worry about conflicting changes.
One of the key features of Git is its distributed nature, which means that each user has a complete copy of the repository on their local machine. This makes it possible to work offline and collaborate with others without needing a centralized server.
Git is also highly customizable, with a wide range of plugins and extensions available to add new features and functionality. It can be used with a variety of different platforms and tools, including GitHub, GitLab, and Bitbucket, which provide additional features for collaboration and project management.
Install and Configure Git
To install Git on your computer, you want to go to the official git website download page and follow the instruction that corresponds to your operating system.
In this tutorial, we will be doing a Linux command line install. If you need to become more familiar with the command line interface, you want to read getting started with the command line interface before you continue.
Installation
- Open a terminal window.
2. Enter the following command to update and upgrade your repository
sudo apt update && sudo apt upgrade
3. Enter the following command to install Git:
sudo apt-get install git
4. Once Git is installed, you can verify the installation by running the following command, which should display the version of Git that you have installed.
git - version
Configuration
- Configure Git with your name and email address by entering the following commands:
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
Make sure to replace “Your Name” and “your.email@example.com” with your actual name and email address.
2. By default, Git will create a branch called master when you create a new repository with git init. From Git version 2.28 onwards, you can set a different name for the initial branch. To set main as the default branch name do:
git config --global init.defaultBranch main
3. You can now check your configuration settings, by running:
git config --list
This will list all the configurations that have been set up to this point.
Basic Git Commands
Git status
This command checks the status of git in the current repository that you are in. The first thing you want to check is if git has been initialized in that directory. For one to use git to track changes in a repository, git has to be initialized.
Let’s get started using the git status command:
- Open the terminal with [ ctrl + alt + T ] on your keyboard.
- Confirm you are in the Home folder with the pwd command.
pwd
3. Move into the Document folder using cd.
cd Documents
4. Create a new folder called coding tools using the mkdir command. Use the -v flag to print out an output of the command.
mkdir -v coding_tools
5. Open the coding tools folder with vs_code.
code coding_tools
6. Open terminal inside vs_code. To do this go to the vs_code toolbar at the top, select terminal, and on the drop-down menu select new terminal. You can also use the shortcut [ctrl + shift + `] to achieve the same results.
7. Check the git status of the coding_tools folder using the git status command.
git status
8. You should get the following response indicating that git has not been initialized in that directory.
fatal: not a git repository (or any of the parent directories): .git
Git init
As stated previously, for git to start tracking changes, git has to be initialized. This is done with the git init command that creates a new .git subdirectory in the current directory that contains all of the necessary files for Git to start tracking changes to your project.
To use the git init command:
- Confirm that you are in the coding tools folder with the pwd command
pwd
2. Run the git init command
git init
3. You should get the following response.
Initialized empty Git repository in /home/themachine/Documents/coding_tools/.git/
4. Check that a new git subdirectory has been created using the ls -a command. If git has been initiated you should see the following response in your terminal.
. .. .git
Git add
Git add is a command used in the Git version control system to add changes made to a file or directory to the staging area, which is a temporary storage area before committing those changes to the Git repository.
To use git add command:
- Create a new file inside coding tools with the touch command and name it “the_command_line_interface.md” as follows
touch the_command_line_interface.md
.md = Markdown > Markdown is an easy-to-use markup language that is used with plain text to add formatting elements (headings, bulleted lists, URLs) to plain text without the use of a formal text editor or the use of HTML tags. Markdown is device agnostic and displays the writing format consistently across device types.
You can read some more about it here.
2. On the vs_code explorer, double-click on your file to open it.
3. Check the git status of your file now and you should see the following response indication there are untracked changes in red.
On branch main
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
the_command_line_interface.md
nothing added to commit but untracked files present (use "git add" to track)
4. Now run the git add command with the name of the file as follows
git add the_command_line_interface.md
5. When you now run the git status command, you should see that the file has been staged and has changed to green.
On branch main
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: the_command_line_interface.md
Git commit
Git commit is a command in the Git version control system used to save changes made to a file or a group of files to the Git repository. A commit is like a snapshot of the current state of the code at a given time, and it contains a message describing the changes made.
To use the git add command:
- Now that your file has been staged, you can go ahead and commit these changes using git commit.
- Run the git commit as follows:
git commit -m "initial commit"
The -m option specifies the commit message, which should be a brief and informative description of the changes made in the commit. An initial commit message is usually used for the first commit in a document.
3. You should then get the following output indicating that a commit has been made.
[main (root-commit) 3f1b7fd] initial commit
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 the_command_line_interface.md
4. When you run the git status command, you should see that there is nothing to commit.
On branch main
nothing to commit, working tree clean
Git log
Git log is a command used in the Git version control system that displays a log of the commit history of a repository. It shows the commit messages, authors, dates, and commit hashes for all the commits in the repository. The most recent commits are shown first.
To use the git log command:
- Confirm that you are still inside the coding_tools folder with pwd command
pwd
2. Run the git log command
git log
3. You should see the following response indicating your most recent commit.
commit 3f1b7fd1e2ba82332ac5dbe2ace30c6bc27aa48b (HEAD -> main)
Author: themachine <themachineke@gmail.com>
Date: Tue Mar 28 03:58:49 2023 +0300
initial commit
The first line is the commits’ unique hash that is used to track that change. The second line identifies the person and email of the person who made the change also known as the author. The third line indicates the date and time of the changes made to the document. The last line is the message indicating what change was made to the document.
4. To exit the log messages press q, for quit.
Conclusion
The above examples show you how to get started with the git version control system. There is however much more you can do with it. For more about that, you can check out the documentation page where there is a reference manual for all git commands and how to use them.
Also under documentation, there is the Pro Git book written by Scott Chacon and Ben Straub is available to read online for free. This is a very good resource to help you understand git.
Lastly, if you prefer learning from youtube, you can check out this beginner crash course by freeCodeCamp.