According to the official documentation, git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency. It was invented by Linus Torvalds in 2005, also the creator of linux kernel. Version control is a system that records changes to a file or set of files over time so that we can recall specific versions later. Basically, it tracks every changes with the code and maintain different versions of that code based on the changes.
We have been working on the codebase for our application which is saved in a local machine. While doing the development work, there are frequent changes implemented in the codebase. Suppose, something bad happens with our computer disk - disk is corrupted, in that scenario, our whole project codebase is lost. It will cause a lot of distress and panic to the developers who is working on the project as they need to start all over again. To mitigate this, we can of course create a regular backups of the project codebase and store that in another disk or somewhere in the cloud storages. But the problem with this approach is, in big projects, the code changes happens so rapidly that this will become impossible to maintain over time and there will always be higher chances of some code loss. Also, we cannot see the history of changes with the application files easily. Another major problem is, in a project, usually multiple persons are involved to develop features and we need to constantly sync the codebase between all the team members. With the above approach, it will be extremely difficult and time consuming to maintain that sync. So, to solve all these problems and many others, we need git.
Git is one of the most widely adopted technology in the world and used by all the developers. The reason for such popularity is:
- It is open source.
- It is cross-platform.
- It is very easy to learn and implement.
To install git, run the following command:
sudo apt-get install git -y
sudo yum install git -y
apk add git
To install in other versions of linux machines, go to the following link:
On macOS, we can install git using homebrew.
brew install git
For windows, go to the following link to download and install git.
Now that we have installed git in our local machine, check the version using below command:
If you see the version number in the output, then we are good to start using git in our machine.
First, we need to configure git with some custom settings so that it will recognize our identity. Let's set user's name and email address as every git commit uses these information.
git config --global user.name "John Doe" git config --global user.email email@example.com
Replace John Doe and firstname.lastname@example.org with your own name and email respectively. We can also set this information on a project repository basis but setting it globally is easier and also recommended. Whenever we need to overwrite this information in a specific repository, we can just issue following commands from the root of that repository directory:
git config user.name "John Doe" git config user.email email@example.com
We can issue following commands to verify the above values:
git config user.name git config user.email
Also, to view all the git configuration values, issue the following command:
git config --list --show-origin
Now that our git setup is ready to use, let's create a git repository. A git repository is the virtual storage for our project. We store all of our code in a git repository and it tracks and maintains every changes made to those code files, in a directory called .git (.git is a hidden folder which contains all the metadata and versioning information about the repository).
To host our git repositories, we have many platforms. Some of the most popular ones are listed below.
Founded in 2008 and currently owned by Microsoft, it is the most popular code hosting platform for version control and collaboration.
Founded in 2011 and currently owned by Gitlab Inc., it is also one of the most popular code hosting platform.
Founded in 2008 and owned by Atlassian, it is also one of the most popular code hosting platform.
All of the above ones are free to use with certain limitations and mostly serves our purpose. If you want to use more enterprise features with less limitations, there are various pricing plans.
Let's use github for this course. If you are new to the github, create an account with github first.
- Go to https://github.com/
- Click on Sign Up button to create an account.
- Once account is created, we are ready to create a new repository to host our code in github. Login to the github account and after login, you will see following screen:
- Click on Create a new repository button
- Provide the name of the application along with its description. You can choose to make your project public or private as according to your wish. I will skip rest of the other configurations for now to create a empty repository with no files on it. We will create each of those README.md, .gitignore, LICENSE later.
- Click on Create repository button after filling necessary informations.
- You will see following screen with a list of very much helpful commands that we need to execute to setup the repository in our local machine.
- For our case, since we already have a project directory ready with application files and folders, let's issue the following command to turn this directory into a git repository.
Navigate to the root of the existing project directory:
From the root of the project directory, issue the command:
We run this command only once in a project root directory while setting the project with git for the first time and it will create a .git folder in the current working directory.
- Since we already have all the files and folders ready for the travel application project, we need to follow the process for existing project as shown in above image. We need to first set the remote url for our git repository.
git remote add origin https://github.com/nodexplained/travel-application.git
Here, we are adding origin as the remote name and the https://github url as the associated remote url for our repository. Remote name doesn't have to be origin, it can be anything but using origin as remote name is pretty much standard and almost used by everyone. Adding remote with https protocol will require username and password verification on each git push command.
Using SSH public key authentication is the most secure form of authentication, an alternative to username and password authentication. An alternative of above git remote add command is shown below which is authenticated using SSH public key
git remote add origin firstname.lastname@example.org:nodexplained/travel-application.git
To verify that remote URL has changed, issue the following command:
git remote -v
Adding remote with ssh protocol requires some additional configuration changes in github settings section. This will saves us from typing username and password every time when we want to push our changes to the remote git repository.
- Click on the settings menu from the profile section or navigate to the https://github.com/settings/profile
- Click on the SSH and GPG Keys menu from the left sidebar or navigate to the https://github.com/settings/keys
- Click on the New SSH Key button or navigate to the https://github.com/settings/ssh/new
- To get SSH Keys, navigate to the ~/.ssh/ folder and check if there exists files - id_rsa and id_rsa.pub or id_ed25519 and id_ed25519.pub, if either of the pair exists, copy content from the existing .pub (id_rsa.pub or id_ed25519.pub) file. .pub file is the public ssh key file.
cd ~/.ssh ls
If those files do not exists, then we need to generate a SSH key using following command:
ssh-keygen -t ed25519 -C "email@example.com"
Modify firstname.lastname@example.org with your real email address. Press enter button for all the prompts without typing anything.
This generates id_ed25519 and id_ed25519.pub files in the ~/.ssh directory. If this command with Ed25519 algorithm is not supported in your system, then run the following command:
ssh-keygen -t rsa -b 4096 -C "email@example.com"
Modify firstname.lastname@example.org with your real email address. This generates id_rsa and id_rsa.pub files in the ~/.ssh directory.
- Give the appropriate title for the ssh key - ex: nodexplained-ssh and copy the id_ed25519.pub or id_rsa.pub content and paste in key textarea.
- Once you click on Add SSH key button, it will prompt for the account password. Provide the github password for security confirmation to finalize the setup of ssh key with github.
To create a branch, we can use git checkout command. main is the default branch name. In git, we usually work with various branches and main is the one primarily used for production deployment. We can create as many branches as we want based off of some other branches. Basically, we create different branches for new features and then merge the changes from those branches to the main branch before deploying to the production server. With the help of multiple branches, we can have multiple versions of the code at a same time.
git checkout -b main
-b in git checkout command causes a new branch to be created if it doesn't exists else throws error saying branch already exists.
Every files in our project directory is marked by git as one of the following:
- tracked - a file which is either added or committed to the git
- untracked - a file which is neither added nor committed to the git
- ignored - list of files to be ignored by git
.gitignore file is one of the most important file we need to create while using git. In any software projects, there are files/folders which we want to share as well as not share to anyone(keep some files within our local project directory only). So, to those files, which we don't want git to add and track, we need to mention the name of those files/folders in .gitignore file. This file should be created at the root of the project directory so that it can tell git which files/folders should be ignored.
Let's create a .gitignore file:
In our project directory, we have node_modules directory which contains all the npm packages that our project requires. This folder is usually of very huge size and we don't want this to be added to the git repository as we can easily generate this folder using npm install command. So, let's add node_modules to the list.
Also, there are some files/folders which may be created by the text editor on the root of the project directory. We want to also ignore those files/folders. Let's mention code editor specific list in the .gitignore as well, which in our case is Visual Studio Code.
We also should not include any sensitive informations in the git repository. Include those files as well in the list.
# Dependency directories node_modules/ ### VisualStudioCode ### .vscode/* !.vscode/settings.json !.vscode/tasks.json !.vscode/launch.json !.vscode/extensions.json !.vscode/*.code-snippets # Built Visual Studio Code Extensions *.vsix
Please note that, git will only ignore untracked files/folders. To ignore files/folders that are already tracked, we need to run the following command:
git rm --cached FILENAME
git rm --cached FOLDER_NAME -r
Please refer to the following links to get the exhaustive list of .gitignore files:
To add files to the git, we can use following command:
--all or -A specifies, all the files/folders that are not mentioned in the gitignore, add those all files/folders to the git. This will take a snapshot of the contents of all files residing in current directory and then is stored in a temporary staging area which git calls the index.
git add --all
We can also add a particular file/folder:
git add FOLDER_NAME
git add FILE_NAME
Once a file/folder is added, we need to commit those changes to the git. When committing the changes, we also need to describe what those changes are for using -m option(for specifying commit message):
git commit -m "travel application initial setup files"
Only after git commit command is executed, it permanently store the contents of the index in the repository. After code commit, it will start to track the changes happening with the code and maintain a different versions of those changes.
Now, the last step is to push all those committed changes to the remote git repository. Here, origin is the name of the remote code hosting server which points to the code hosting url behind the scenes and main is the branch name.
Since, we don't have a branch created in the github repository as the main branch only exists in our local repository, we can issue following command to push our changes along with the branch name to the remote github repository and then asks the local main branch to track the remote main branch.
git push -u origin main
For the future changes, you can issue following command to push the changes to the remote github repository.
git push origin main
To check the git status - it will display status of the files, shows file with modifications, shows new file list if any, shows deleted file list if any:
The recommended approach is to add the files individually to the git and commit with proper message describing the type of modifications. So, from next time, add the files to the git individually with proper commit message.
In our next chapter, we will discuss about docker and also setup a basic dockerfile for our project.