Day 8: Git & Github Basics For DevOps Engineers

Day 8: Git & Github Basics For DevOps Engineers

What is Git?

Git, developed by Linus Torvalds in 2005, is a widely-used version control system for software development. It helps developers track changes to their code, collaborate with others, and manage complex codebases. Git uses a distributed model, allowing each developer to have their local copy of the code repository to work on, and provides tools for branching and merging code to prevent conflicts and ease the management of large codebases.

What is GitHub?

GitHub is a popular web-based platform that offers developers a centralized location to host, share, and collaborate on their code projects using Git version control. It comes with a variety of features like issue tracking, pull requests, and code reviews, which enhance the development process's efficiency. By using GitHub, developers can contribute to open-source projects, collaborate on private projects with their teams, and showcase their work to potential employers. With millions of users and repositories, GitHub has become a top choice for developers to share, explore, and learn about code, as well as a community hub for the software development industry.

Difference between Git and GitHub

FeatureGitGitHub
TypeVersion Control System (VCS)Web-based hosting service for Git repositories
PurposeHelps track changes to code and manage versions provides.It provides a centralized location for hosting, sharing, and collaborating on Git repositories
CollaborationCan be used for collaboration, but requires additional tools and setup built.It can be useful in built-in collaboration features like pull requests, code reviews, and issue tracking
AccessCan be used offline or on local networks, requiresIt requires an internet connection to access public
Public/ Private RepositoriesCan be used for both public and private repositoriesSupports both public and private repositories
User ManagementDoes not have built-in user management featuresIncludes tools for managing user access and permissions
CommunityHas a large user community and is widely used in the software development industryHas a large user community and is a popular hub for open-source projects
PricingFree and open-sourceOffers both free and paid plans with additional features and storage options
Additional FeaturesDoes not have built-in features for issue tracking or code reviewsIncludes features for issue tracking, pull requests, code reviews, and more

What is version control?

Version control is a system that helps manage changes to files or code over time. It allows users to track and compare different versions of a file, revert to previous versions, and collaborate with others on a project.

How many types of version controls do we have?

There are three main types of version control systems: local version control systems, centralized version control systems, and distributed version control systems.

  1. Local Version Control Systems:
  • Stores versions of files on a local machine.

  • Maintains a database of file changes.

  • Easy to set up and use.

  • Limited collaboration features.

  • Examples include RCS (Revision Control System) and SCCS (Source Code Control System).

  1. Centralized Version Control Systems:
  • Stores versions of files on a central server.

  • Provides access control and version history for multiple users.

  • Changes are made on the central server.

  • A network connection is required to access files.

  • Examples include CVS (Concurrent Versions System) and SVN (Subversion).

  1. Distributed Version Control Systems:
  • Copies of the entire repository are stored on each user's local machine.

  • Changes can be made locally and committed to the repository.

  • Allows for offline work and collaboration.

  • More complex than local and centralized systems.

  • Examples include Git, Mercurial, and Bazaar.

Why do we use Distributed version control over centralized version control?

Distributed Version ControlCentralized Version Control
Each user has a complete copy of the repository on their local machineAll users access a central repository on a server
Offline work and collaboration are possibleA network connection is required to access files and collaborate
Changes can be made and committed locally, then synced with the central repositoryChanges are made on the central repository and then pushed to other users
Branching and merging is easy and flexibleBranching and merging can be more difficult and less flexible
Examples include Git, Mercurial, and BazaarExamples include SVN (Subversion) and CVS (Concurrent Versions System)

Distributed version control is preferred over centralized version control due to several advantages, such as:

  • Offline work and collaboration: Users can work and make changes to their local copy of the repository without needing to be connected to the central server.

  • Flexible branching and merging: With distributed version control, creating and merging branches is easier and more flexible than with centralized version control.

  • Better redundancy and security: Each user has a complete copy of the repository on their local machine, providing redundancy and a backup in case the central server goes down or data is lost.

  • Faster performance: Users can work and commit changes faster since they access their local copy of the repository.

  • Examples of distributed version control systems include Git, Mercurial, and Bazaar, while centralized version control systems include SVN (Subversion) and CVS (Concurrent Versions System).

Hands-On:

Step 1: Install Git on your system

You can use this link to install git on your local system https://git-scm.com/downloads

you can also use AWS ec2 instance to install git using the below command:

Sudo apt-get install git

Note: I am using git bash on my Windows machine and working with git on my local

Step 2 : Check the git version using the below command:

git --version

Step 3 : Configure your username and email id:

Step 4: Create a GitHub account by browsing github.com

Step 5: To work with git on my local machine I will initialize the git using "git init" in my present working directory:

Run ls -la and you will see a ".git" folder/directory which indicates that this folder git-demo is now a repository in your local machine

Note:

There is a journey of a file stored in your file system which stores in your git also known as a tool for the version control system. Let's understand that journey.

Terms in this journey are:

  • Workspace: This is where you create, modify, and delete files on your local machine.

  • Staging area: This is where you prepare files for committing to the Git repository by selecting and staging changes.

  • Repository: This is where the staged changes are saved permanently, and the entire history of the project is stored for future reference and collaboration with other developers.

To summarize, files are initially created, modified, and deleted in the workspace, then selected and staged in the staging area, and finally committed to the repository for permanent storage and version control.

Step 6: Create a file git-test.txt

note : this file is currently in your file system and not in your repository

Step 7: Check the status of the git

note : now git shows it has 1 untracked file which will be adding to the git staging area

Step 7: Adding the file to the git

step 8: Now our file is ready to commit and we will be committing the file to the git.

note: I have run git status again to see if we have any commits left and it shows the working tree clean which means there is nothing left to commit

Step 9: we will git log command to see if the commit ID has been created:

step 10: Let's modify the file by putting some content to it and see how a version is created in git

note: I have modified the file and checked the status and it shows

Step 11: I will add the file and commit it to the git repository:

Step 12: we will check the git log again to see if there is a new commit id for this version

note: we can there are 2 commit IDs with different version:

Step 13: Connecting my local repository to the remote repository

note: first check if there is any remote repository there.

Step 14: we need to create a repository by the name of your local repository in GitHub. In my scenario, I have a remote repository by the name of "git-demo".

Step 15: Adding the central repository to your local and checking the status:

Step 16: Now we need to push our local changes to the remote repository from the local repository but for the same we need personal access token which we need to create

Go to settings>developer settings>personal access token>token(classic)

Step 16: Pushing the local repository to the remote

Step 17: Check if the file is pushed

Step 17: Create a repository test in GitHub and clone it to your machine

Step 17: Create Readme file, add it and commit it to the git

Step 18: Pushing the file to the remote repository

note: file has been added successfully:

In the next lesson we will deep dive into Git and learn some another features with hands-on.