Hello friends, here comes the basic tutorial to learn git from scratch. This is part 1 of the tutorial “Getting started with Git Step by Step Part 1” and I will cover version control system – advantages and drawbacks, types of version control system, git glossary – master, branch, head, remote, origin, clone, push, pull, git merge, installing and configuring git on windows OS, installing git on linux/unix and git files states. Interesting part of this tutorial would be that every concept is explained with commands and screenshots. Even feature image is created in such a way that readers gains a lot from the feature image itself. I will also make the second part of this must know management system, so please stay tuned with us. I also hope that readers will first go to the table of contents so that they will know what they are going to learn till the end. I will also add additional references in the end so that readers can learn from those also. At the end of this interesting and educative blog readers will learn about the basics of git in a precise way.
Table of Content
- Version Control System – Advantages and Drawbacks
- Types of version control system
- Git Glossary – Master, branch, head, remote, origin, clone, push, pull, git merge
- Installing and Configuring Git on Windows OS
- Installing Git on Linux/Unix
- Git Files States
- Bottom Line
Version Control System – Advantages and Drawbacks
One must understand what is version control system and why one must use it. It is a management tool which saves the version of your project. These versions help one to get an idea what are the changes made, when these were made and who made this. Suppose one get struck in some part of the code, he/she may refer to the previous version to get an idea about the problem. One of the another salient and key features of this management system is that it does not depend on technology/framework you are using or type of project you are working. Let us see two major benefits of a version control system and then we will dive into the drawbacks of this.
- It allows to compare the files and hence keep track of applications build.
- Trouble shooting the error as all the versions of the application are preserved.
- In VCS, there is a centralised server where all these versions are kept. Suppose it goes down for an hour, then at that time no one can collaborate or save version changes they are working on it.
In short, I am mentioning some points why VCS is to be used. These are collaboration (team can work independently on a project and at the final changes are merged in a single version) , storing version (it stores all the version that you make), branching and merging (developers can work on different branches and finally merges the content to the master branch), backup (cloning the copies of the code from remote to the local machine and hence serves as a backup if the central server crashes for some reason)
Types of version control system
There are two main types of version control system which are mentioned as below
Centralized VCS: It has the single server which keeps all the versions. For example CVS, Subversion and Perforce.
Distributed VCS: In these systems every client has full backup of the data (remember in case of centralized only the latest snapshot of the file is check out) so even if the server crashes, the whole data can be pulled back to the remote repository from local.
Git Glossary – Master, branch, head, remote, origin, clone, push, pull, git merge
Master: Whenever you create a git repository, this branch is created by the git. ThiInstalling and Configuring Git on Windows OSs is also called the source of truth. It is default branch name in git. Please see the below screenshot for understanding my point
Branch: Branching allows you to work on separate copy of the code without affecting the master branch. In this way you may experiment with the code, add new features and if everything works well you can merge the changes back to the master branch. You can clone the repository locally using git clone and execute the below statement to know the branches in your remote repository you have created.
Viewing branches in the local system
Step 1: First clone repository. For example executing the below command will clone my repository
git clone https://github.com/AISangam/Getting-started-with-Git-Step-by-Step.git
Now go to your cloned repository folder and execute the below command to see branches in your remote repository (remote branches)
git branch -r
Please see the screenshot for this as below
One can also see the branches from your remote repository. Please see the below screenshot
Head: HEAD is a reference to the last commit in the currently check-out branch. Please see the below screenshot for better understanding.
Remote: These repositories are hosted on the internet. This is very important when one needs to push or pull changes from the local to the remote and vice versa. To know your remotes please execute the below statement.
To see the url of the cloned repository along with the remote name, please execute the below command
git remote -v
Let us see how remote is very useful. Suppose you have updated the github readme file for your repository. Now you wish to update the readme file at your local system also. To do so, please go to the cloned folder in the local system and execute the below command
git pull origin
Please see the screenshot to get it more clear
If you are facing error of overwritten by merge then execute the below command
git reset --hard git pull origin
Clone: Cloning refers to copying the repository from remote to local. Please remember that even if you have selected any other branch (other than master) and executing the below statement
git clone url_of_repository
It will clone master branch. To clone the specific branch please execute the below statement in the terminal.
git clone -b **name_branch** url
git clone-b first_branch https://github.com/AISangam/Getting-started-with-Git-Step-by-Step.git
pull: pull is used to fetch the content from the remote repository to the local system. First of all, clone the repository you want in your system. You can clone either master branch or any other branch. Now when you make the new changes in that branch, please execute the below command in the local system. You must be inside the cloned folder in the local system
git pull origin
push: It refers to push the code from the local system to the remote repository. Please execute the below command.
git init - This will initialize the local directory as git directory git add . - It adds the files in the local repository and stages them for commit git commit -m "message" - This will prepare the files to be pushed to the remote repository git remote add origin remote_repository_url - This will add the url for the remote repository where the local code will be pushed git remote -v - verify remote url git push origin master - push the code to the github from local system
If you want to undo the pushed files, please execute the below command
git push -f origin HEAD^:master
git merge: merge refers to adding files from the other branches to the master branch. Once you have done some experiments with the code in other branches, you can merge the changes in the master branch. I am adding the screenshots for these if you want to do from remote repository
Command line steps for this process
- Clone the **other branch** in your local system
- Execute the command git checkout master to get switched to a new branch ‘master’
- git merge **other branch**. Please type the name of other branch which you want to merge.
Installing and Configuring Git on Windows OS
Visit the link https://git-scm.com/download/win. Your downloading will start. Please see the screenshots during installation.
Check the git version by executing the below command in Git Bash
Please also look at the screenshot for the final installation
Now Configure username and email. Please see the below screenshot how to do so.
Now we are finished with installation github windows.
Installing Git on Linux/Unix
To install git on linux, please run the below command
sudo apt-get install git
Please see the below screenshot
Installing Git Linux and Installing Git windows is finished. I hope readers are following me. We have finished with VCS, git glossary and installing git on Linux as well as windows. In ubuntu also if you have configured username and email properly like we have done for windows, you will see the below details. Please see the screenshot
Git Files States
There are two types of files states which are mentioned as below
Untracked: Files and directories which are newly created are by default untracked. If we want some files not to be tracked by git, we want to keep them as untracked. Such files includes application log files.
Tracked: Any file which is tracked, git will track the changes automatically. By adding the command add untracked files is changed to tracked files
Let us understand untracked files using the below example. Create a new folder using the terminal and execute the below command in it.
git init: This will make folder work as git repository (for local system). When this command is executed a folder .git will be created inside your working folder. Please see the screenshot for this
Now execute command git status and one will get the file states. Please see the below screenshot
Let us bring the untracked file into the tracked category by executing the command git add multiply.py. Please see the below screenshot.
Using git add command files comes in the staging area and now it is the time to commit the files. Please execute the below command to commit the files to the remote repository
git commit - m "initial commit"
Since, file multiply.py has been tracking by git if i make some changes in the file and run command git status, it will let us know that file is modified. I had added a new line to the file multiply.py and run the command git status. Please see the below screenshot to see the effect of this command to file multiply.py
Now file has been tracked , the next step would be to move the file to the staging area. As we have done before please execute the command git add multiply.py to move it to the staging area.
Execute the command git commit – m “new line is added in file multiply.py” to commit the file. See the below screenshot
I hope the readers are enjoying three things in this tutorial. First getting knowledge about git and version control systems. Second, writing the commands to implement the things practically and thirdly understanding more better from the screenshots. In a nutshell, we have covered the basics of git glossary in this tutorial. Such glossary is very important when one needs to practice the git. We have also learned how to install git on both windows as well as unix. This we have done with proper screenshots and commands. I forgot to mention that we have also gone through centralized as well as distributed version control system in the starting. At the end, we have gone through file states in git by creating the git repository at the local computer. Meanwhile I am also planning to create the part 2 of this must know management system. If you want to ask any queries ir wants to provide us with any feedback, please do at firstname.lastname@example.org. You can also see some of the demo of our data science solution company at our official AI Sangam You Tube Channel. If you wish you may also subscribe to our channel and will get more updates about machine learning, deep learning, docker, django, flask and many others must know domains of coding, You can also communicate with us through skype: aisangamofficial. I hope readers got some knowledgeable points from this tutorial. We will keep on writing such tutorial in the future also.
Since readers have read this tutorial, it is important to visit the second part of this interesting and knowledgeable series where readers will come to know about .gitignore, git diff, git checkout and git reset HEAD. I am attaching the link at the end of this tutorial so that readers can gain better insights and learn git in depth.