You are currently viewing Getting started with Git Step by Step Part 1 | AI Sangam

Getting started with Git Step by Step Part 1 | AI Sangam

Loading

Introduction

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

  1. Version Control System – Advantages and Drawbacks
  2. Types of version control system
  3. Git Glossary – Master, branch, head, remote, origin, clone, push, pull, git merge
  4. Installing and Configuring Git on Windows OS
  5. Installing Git on Linux/Unix
  6. Git Files States
  7. Bottom Line
  8. References

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.

Advantages

  • 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.

Disadvantages

  • 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
  • Distributed

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

Default branch in Git: Master Branch
Default branch in Git: Master Branch

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

Viewing the remote branches from local system
Viewing the remote branches from local system

One can also see the branches from your remote repository. Please see the below screenshot

Branches in remote repository
Branches in remote repository

Head: HEAD is a reference to the last commit in the currently check-out branch. Please see the below screenshot for better understanding.

Heads of different branches
Heads of different branches

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.

git remote

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

pulling changes from remote to local
pulling changes from remote to local

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.

Demo example

git clone -b **name_branch** url

Practical Example

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

Merging the other branch to master branch using pull request
Merging the other branch to master branch using pull request
if no conflict merge pull request
if no conflict merge pull request

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

Installing Git on Windows
Installing Git on Windows

Please also look at the screenshot for the final installation

Installation in Progress
Installation in Progress
git --version

Now Configure username and email. Please see the below screenshot how to do so.

Configure username and email in Git Bash
Configure username and email in Git Bash

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
Installing Git Linux

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

Configuring email and username in ubuntu (Git)
Configuring email and username in ubuntu (Git)

Git Files States

There are two types of files states which are mentioned as below

  • Untracked
  • Tracked

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

git init
git init

Now execute command git status and one will get the file states. Please see the below screenshot

Untracked files
Untracked files

Let us bring the untracked file into the tracked category by executing the command git add multiply.py. Please see the below screenshot.

Tracked file using git add
Tracked file using git add

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
git commit
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

git status after modifying tracked file on local system
git status after modifying tracked file on local system

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.

git add to move the tracked file into the staging area
git add to move the tracked file into 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

git commit after staging
git commit after staging

Bottom Line

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 aisangamofficial@gmail.com. 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.

References

Up and Running with Version Control Systems

git–distributed-even-if-your-workflow-isnt

SVN, Git Out of Here: How to Choose Your Version Control System

A full tutorial on how to use GitHub

Working with remotes

gitignore, git diff, checkout and reset HEAD Git Part 2 | AI Sangam

This Post Has 26 Comments

  1. Hemant

    While working on my github remote repository, I did not know how to add collaborators to my repository. Can you please guide me regarding this.

    With regards
    Hemant

  2. Amrita

    Hello AI Sangam, I hope you are fine. I have a question regarding git. Question is as below

    I have created the remote repository and then cloned it to the local system. I have created some file in the local system but unable to push it to the remote repository. Can you guide me regarding this please.

  3. Rajesh

    Rajesh this side,

    I had a question while pushing the work to the remote repository, I got the following error

    “failed to push some refs to repository”

    I am unable to solve this, Can you help me out.

  4. Kanu Singh

    Hello Kanu Singh this side

    I got the error named as automatic merge failed (github) whule pushing the changes from the local system to remote. Can you please help me how to solve this. I am very confused why it happened.

    1. AISangam

      Thanks for showing interest in us. I too had faced these issue while working with git hub and version control system. I had created a post for this so that others can also take benefit from this and learn in more precise way. Please have a look to the post by visiting this link http://www.aisangam.com/fixerrors/viewtopic.php?f=4&t=23

  5. Gurinder

    Hello sir, How are you. You right with proper care and is well documented. My question is whether git pull and git fetch are same or there is some difference between them. Could you please explain me.

    1. AISangam

      Thanks for asking. In simple words git pull = git fetch + git merge. I hope this would clear the doubt in your mind.

      With regards
      AI Sangam

  6. Komal

    Hi, I had gone through your tutorial and had found nothing related to creating the branch locally. I need this as I am working on local system and I need to shift to the newly created local branch so that once the code is okay I can merge the code to the master branch.

  7. Rishi

    Hello AI Sangam. Rishi this side. It is an exhilarating experience to read this blog especially comment section. Immense blessings from you as I gained a lot after reading this blog as well as comment section. It makes my learning towards VCS (version control system) latched as well as imparted better understanding of the github commands

    I would like to disentangle my knowledge towards git tag as it is very important command before code deployment. It would be great if you provide me ample knowledge about this aspect also.

    1. AISangam

      Thanks a lot for asking this question. I had already answered this question on AI Sangam forum (http://www.aisangam.com/fixerrors/). I hope if you read the answer from this link carefully better as well as explicit understanding will be imparted to you.

      http://www.aisangam.com/fixerrors/viewtopic.php?f=4&t=37

      I would also love to tell you here that you must also learn about git stash which saves the changes temporary. In this way you can be free for committing the code for some time and when you feel everything is okay you can commit the changes. Please execute the below code for stash

      git stash save "name_of_modified_file"
      

      You will get message like this

      Saved working directory and index state On master: modified A.txt
      HEAD is now at 2cf7aeb merging the changesSSSSSS
      
      You can also see the items stored in the stash in form of dictionary. Run the below command
      
      git stash list
      

      You will get output like this

      stash@{0}: On master: modified A.txt
      

      With Regards
      AI Sangam
      https://www.youtube.com/channel/UC9x_PL-LPk3Wp5V85F4GLHQ/about?view_as=subscriber

  8. Geeta

    Hello AI Sangam, I would like to ask you how to delete the branch forcefully. I don’t need to merge the changes from the local branch to the master branch (on local system). But git does not allow me to delete the branch forcefully.

    With Regards
    Geeta

    1. AISangam

      Thanks for reaching to us. To delete the branch forcefully, please use the flag D instead of d. Please see the below command how to do so

      git branch -D *name_of_branch*
      

      Also please don’t forget the golden rule of coming out of the branch to delete it.

      With Regards
      AI Sangam

  9. Kunal

    I would like to know about creating local branches, merging the changes at the local computer from the local created branch to the master branch at the local system and finally to the remote.

    I hope you would hove my knowledge about git and will provide better insights with respect to these questions which I have asked above

    With Regards
    Kunal

    1. AISangam

      Thanks for such question. To unravel such doubts in minds, I would like you to visit AI Sangam Fix error forum and particularly https://www.aisangam.com/fixerrors/viewtopic.php?f=4&t=32

      If you would see the discussion, you would come to know how to create the local branches, merging the changes at the local computer from the local created branch to the master branch at the local system and finally to the remote.

      I hope this discussion will provide you ample knowledge with respect to question asked by you. AI Sangam tries to impart best we can so that your coding and learning skills become more pragmatic and explicit.

      With Regards
      AI Sangam

  10. Harpreet

    Hello AI Sangam, I hope you are well. I appreciate you for writing such an blog as it has removed precarious feeling to learn git. Especially from the blog as well as comment, I do understand a lot of concept in a precise way. From your forum https://www.aisangam.com/fixerrors I am gaining a lot of knowledge and it curated my knowledge in an structured way.

    I would like to ask you what would be my approach if I would like to delete the local branch both from the local repository as well as from remote. I am asking such an pragmatic question because when changes are merged to the master branch it would be safe to delete the other branch when not needed. I tried with some options but I would get the error as

    “could not delete the branch which you are currently on”

    I hope you would answer this question

    1. AISangam

      Hello Harpreet, Thanks for your encouraging words. Much appreciated from AI Sangam. Please remember one golden rule to delete the branch

      1. You must be out of the branch which you wish to delete

      Please switch to the master branch for deleting the other branch. Please see the code to switch the branches from below

      git checkout master
      

      Now it will not issue the error

      “could not delete the branch which you are currently on”

      To delete the branch from the local system

      git branch -d **name_of_branch**
      

      To delete the branch from the remote repository

      git push --delete origin **name_of_branch**
      
  11. Kriti

    Kriti here, Can you explain me what is difference between git merge as well as git rebase command. I think both are same.

    1. AISangam

      Hello Kriti, I hope you are doing great. To answer this question let us take an example. Git merge and Git Rebase do merging of changes from one branch to another branch but their way is different. Let us see the difference from below

      Git Merge: It merges changes form one branch to the second branch (from user_created_branch to master branch) by creating a brand new commit which has two parents one from other branch and other from master branch and hence graph does not remain linear

      Git Rebase: This is another git command which stacks other branch commits over the latest commit of the master branch or vice versa and hence graph remains linear.

      I hope you got the answer which you are looking for.

      With Regards
      AI Sangam

  12. Kamal

    Hello AI Sangam, I hope you are doing great. There is a question in my mind as I was learning about git commit chains but I am not fully satisfied with how how git commit has a reference to the parent commit. If you could help me to understand this, it would be great and helpful for me. I know what is the importance of knowing git if you wish to get job in the IT field.

    1. AISangam

      Thanks a lot kamal. Wish you best and hope you would get a good job in the market. I have discussed about git chain commit in my ai sangam fix error page whose link I am adding here. I hope you would visit this link and will get better insights of what you actually need. Please see the link from below
      http://www.aisangam.com/fixerrors/viewforum.php?f=4

  13. Aarti

    Hello AI sangam, I went to all the comments which are discussed above. My question is that how git merge command works.

    1. AISangam

      Thanks a lot for writing to us. Git merge is the command which is used to merge the changes from other branch to master branch and vice versa.

      For example, you have to be add some files in the master branch and then committed such changes. Now you wish to see the changes in the other branch then you need to switch to the new branch and execute the command

      git merge *name_of_branch_which_you_want_to_merge*
      

      Now, the main point is

      you need to be on the branch where you need to merge the changes. Please keep this in mind.

Leave a Reply