Git is the most popular version control system used in the software development industry. But its workflow may require you some time to get familiar with, especially if you haven’t worked with version control before.
This Git tutorial will help you get a good understanding of this useful software and how it can help with your projects.
Git and GitHub Introduction
What Is Version Control?
Also known as revision control or source control, this management practice helps you track and manage changes to source code, documents, and any collections of files.
The whole history of your project is there, including pieces of work that no longer exist in the newest versions. The content of each modification is kept permanently, including its metadata, like who committed that change and when.
A version control system (VCS) is a program designed for this task. Software development is where you can see the most widespread use of those applications. In this case, it is typically used to track the source code of your programs. Another name for it is a software configuration management system (SCM).
You can deploy a VCS into projects that either only one or multiple people work on at the same time. Many team members can edit and change the same file at the same time without interfering with the work of each other.
You can recall any earlier state of any single file or even the whole project. Thanks to version control, it is also easy to create independent lines of development where you have more freedom to work on specific problems or goals.
Why Version Control?
The code base is the soul and most important asset of any software development project. It doesn’t just host the recipe for your final product but also the invaluable understanding and knowledge of the problems you or your team are working on.
Version control is the entire history of the source code. Having this history is crucial to investigating problems and understanding better why your current project is the way it is right now.
When your project grows, you may need to work on different streams of changes (or branches). For example, you can develop a new feature while another member fixes bugs on an older release. Branches ensure you two don’t hinder each other and the whole project.
What Is Git?
Git is the most popular version control system. It was originally created by Linus Torvalds, the creator of the Linux kernel. He developed Git to replace a proprietary VCS previously used in maintaining the development of Linux.
Unlike earlier VCSes like SVN and CVS, Git doesn’t rely on a single centralized server to store the history of a project. Linus took the distributed approach. The entire code base and its full history are also stored in each developer’s workstation.
Distributed VCSes prevent the risk of single points of failure. Even when the main server is down, everyone can continue working on their local copy.
After fetching a copy of the project, you don’t need to connect to the server to edit its source code files when every modification can be sent to the maintainer later. You can quickly make experiment changes and ensure they work properly before sending them to other members.
GitHub is an online service for hosting Git repositories. In addition to standard Git functionalities, GitHub also adds many features to facilitate the sharing and collaboration of software projects.
As the leading name in the industry, GitHub is known for its library of open-source projects. While organizations can choose this service to host their private repositories, GitHub is a popular destination for those who want to publish their work and invite others to improve it. GitHub is a great place to learn Git and collaborate with other developers without having to set up your own server.
As of 2022, there are more than 200 million repositories (28 million of which are public) on GitHub, making it the biggest source code host in the world. You can find projects of every kind on GitHub. This ranges from a short Bash script by an ordinary user to a large project from companies like Google or Microsoft.
Git Getting Started
Git is a command-line application and may come pre-installed in your system. Check whether you already have Git by using entering this command onto a terminal app:
Git is readily available on your system if you see an output looking like this:
git version 2.38.0
Otherwise, your terminal app will give an error “command not found” or so.
Install Git On Linux
Most Linux distributions have Git in their official software repositories. These binary packages are built and provided by your distro’s developers.
Most of the time, you will need to install the package git with the package manager of your system. If your current Git installation is out-of-date, these commands should update it as well.
sudo apt-get install git
sudo dnf install git
sudo zypper install git
sudo pacman -S git
Install Git On macOS
There is no official Git binary for macOS, but you can install it from a third-party source.
Users of Homebrew can install Git as a binary package. Install Homebrew first if you don’t have it on your macOS system by entering this command in the Terminal app:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Follow the instructions on the screen. When the installation of Homebrew completes, use this command to install Git:
brew install git
Install Git On Windows
Those who are new to Windows’ command-line interface can use the installer from Git for Windows, a project aiming to provide better integration of Git into Windows.
Open the file you just downloaded and follow the instructions on the screen to install Git. It comes with Git Bash – a Bash emulation that helps you run Git commands in a similar way to Linux and macOS systems.
The commands in this guide assume that you are using Git Bash on Windows. Right-click when you are browsing a directory in Windows Explorer and select Git Bash to open a command-line interface at that location.
You can also go to Git’s official download page and grab the standalone installer for your system.
Basic Concepts Of Version Control With Git
You need to get familiar with Git terminology in order to understand how it works and learn Git commands. Here are some of the most popular terms Git users commonly use:
- Working tree: the set of files and directories you are working on with your project.
- Repository (or repo for short): the directory where Git stores all of the metadata and history of a project.
A repo is typically also part of a working tree, so you can work on a project and manage its Git data in the same place. If this isn’t the case, the repo is used for backup or sharing, and it is called a bare repo.
- Commit: a commit is a snapshot of your project’s working tree at a given time. After making changes to the source code, you will need to commit them (meaning creating a commit), so Git can formally register your changes.
- Branch: a series of commits that typically represents an independent line of development. By initializing a Git repository, you also create the default branch to which you can merge changes from other branches later.
- Remote: a remote is any Git repository that isn’t the repo you are working on.
- Origin: the default remote repo Git uses for operations like push and pull.
Before trying out its basic commands, you will need to provide Git with some basic information (your name and email address, in particular). You only need to do this once in your system.
Use the subcommand “config” (replace NAME and EMAIL with values you want to use):
git config --global user.name "NAME" git config --global user.email "EMAIL"
git config --global user.name LearnShareIT git config --global user.email [email protected]
Verify the information you have entered:
git config --list [email protected] user.name=LearnShareIT
Create Your Git Repository
The primary purpose of Git is to monitor a directory (the working tree) and keep track of changes to files within it. In this tutorial, we are going to set up a Git repository on a new directory to demonstrate this VCS’s capabilities.
Create a new directory with mkdir and change your current working directory to it:
mkdir learnshareit cd learnshareit
Initialize a new Git repository and set the name of its default branch:
git init --initial-branch=main
Git will create a subdirectory named .git inside your current directory to store the repository it just created.
Git New Files
You can add new files to your working tree or modify existing files in whatever way you prefer.
echo "console.log('Welcome to LearnShareIT');" > index.js
You can use Git’s status subcommand to view the current state of your working directory:
git status On branch main No commits yet Untracked files: (use "git add <file>..." to include in what will be committed) index.js nothing added to commit but untracked files present (use "git add" to track)
Git is basically telling you that it notices some changes to index.js, but this file is untracked. This means it hasn’t been added to the staging area and won’t be part of the next commit.
It is important to note that Git uses the edit-stage-commit pattern. After editing your files, you must stage those changes with “git add”, preparing them for the next commit.
Use this command to add index.js to the staging area:
git add index.js
You can now check the status of your working tree again:
git status On branch main No commits yet Changes to be committed: (use "git rm --cached <file>..." to unstage) new file: index.js
Git has now recognized changes in index.js and will add them to the next commit.
If you are happy with all the staged changes, you can prepare the next commit with the subcommand commit. It creates the current state of the project with those staged changes and stores it in a commit of your local repository.
The most popular use of the subcommand commit is the -m option. It allows you to include a message with the commit:
git commit -m "creating index.js" [main (root-commit) 9e35671] creating index.js 1 file changed, 1 insertion(+) create mode 100644 index.js
You can also use the -a option to add all changed files to the commit no matter whether you have staged them with “git add”:
git commit -am "creating index.js"
Branching is one of the most useful concepts of Git. By creating a new branch, you can diverge from the primary line of development and work on a new line without having to create a copy of the source code or messing up the main branch.
You can list all the branches of your Git repository with this command:
This output indicates there is only one branch called main:
The asterisk character is displayed before the branch you have most recently checked out. Your next commit will be added to this branch.
Use this command if you want to create a new branch (“fix”, in this case):
git branch fix
List all the branches again:
git branch fix * main
Notice that you are still on the main branch. Check out the new branch to switch to it:
git checkout fix git branch
Switched to branch ‘fix’
* fix main
You can now work on your branch, and Git will save all new commits to it (unless you switch to another branch).
Let’s insert a new line to index.js:
echo "console.log('This is a Git tutorial.');" >> index.js
Prepare for the next commit:
git commit -am "edit index.js" [fix e2347ce] edit index.js 1 file changed, 1 insertion(+)
Git Branch Merge
Merging in Git means combining commits from different branches into a unified history. You can use the git merge subcommand to merge the fix branch above to the main branch. This is an example of how you can combine short-lived branches into your main line of development.
First, you have to get back to the main branch:
git checkout main
Switched to branch 'main'
Use the merge subcommand to merge it with the fix branch:
git merge fix Updating 9e35671..e2347ce Fast-forward index.js | 1 + 1 file changed, 1 insertion(+)
The previous commit you added in the fix branch has been added to “main”. You can now delete your branch:
git branch -d fix
You can use “git help” to open Git’s official manual for a specific subcommand:
git help branch
To list all available commands:
git help --all
- Git fatal: Could not read from remote repository
- Error “src refspec master does not match any”
- Fatal: detected dubious ownership in repository
- How to delete folder in Gitlab repository
- Git commit your changes or stash them before you can merge
- How to skip Git commit hooks
- Fatal: could not read username for ‘https://github.com’: terminal prompts disabled
- Git cannot be resolved to branch
- Please move or remove them before you switch branches
- How to search github for code
Git Tutorial – Summary
This Git tutorial should give you basic ideas of how Git and version control systems, in general, work. It is a great addition to your development tool when your project grows, and you want to collaborate with other people.
My name is Robert. I have a degree in information technology and two years of expertise in software development. I’ve come to offer my understanding on programming languages. I hope you find my articles interesting.
Name of the university: HUST