Basic Git & GitHub for DevOps Engineers

Basic Git & GitHub for DevOps Engineers

Day 8

What is Git?

Git is a distributed version control system primarily used for managing and tracking changes in source code during software development. It was created by Linus Torvalds in 2005 for managing the development of the Linux kernel but has since become widely adopted across various industries and projects.

Git allows multiple developers to work on the same project simultaneously, making it easier to collaborate and merge changes seamlessly. Each developer has their own copy of the entire project repository, including its complete history, enabling them to work independently and offline. Git tracks and records changes to files and directories, allowing developers to create new branches, make modifications, and merge their changes back into the main codebase.

The key concepts in Git include:

  1. Repository: A repository is a collection of files and directories that make up a project, along with the complete history of changes made to them.

  2. Commit: A commit is a snapshot of the project at a specific point in time. It represents a set of changes made to the repository, such as adding, modifying, or deleting files.

  3. Branch: A branch is an independent line of development in a repository. It allows developers to work on new features or bug fixes without affecting the main codebase. Branches can be merged back into the main branch once the changes are tested and reviewed.

  4. Merge: Merging combines changes from one branch into another. It integrates the changes made in a branch back into the main branch or another target branch.

  5. Pull Request: A pull request is a mechanism for submitting changes to a project. It allows developers to propose their changes to the repository's maintainers for review before merging them.

What is GitHub?

GitHub is a web-based hosting service for version control using Git. It provides a platform for developers to collaborate on projects, share code, and manage software development workflows. GitHub builds upon the functionality of Git and adds features and tools to enhance collaboration and project management.

Some key features and concepts of GitHub include:

  1. Repository Hosting: GitHub allows users to create remote repositories to host their code. These repositories can be either public, where anyone can access and contribute to them, or private, which restricts access to a specific set of collaborators.

  2. Pull Requests: GitHub introduces the concept of pull requests, which enables developers to propose changes they have made in their repositories to be merged into another repository. Pull requests provide a convenient way for code review, discussion, and collaboration before incorporating changes into the main project.

  3. Issue Tracking: GitHub includes an issue tracking system where users can report bugs, request features, or discuss ideas related to a project. Issues can be assigned, labeled, and commented on, providing a central place for collaboration and project management.

  4. Collaboration and Forking: GitHub facilitates collaboration by allowing users to fork repositories. Forking creates a personal copy of a repository under the user's account, enabling them to make changes independently. Users can then propose their changes to the original repository through pull requests.

  5. Version Control and History: GitHub leverages Git's version control capabilities, allowing users to track changes, view commit history, and revert to previous versions if needed. It provides a graphical interface to visualize the commit history and branches of a repository.

  6. Continuous Integration and Deployment: GitHub integrates with various continuous integration and deployment (CI/CD) tools. This allows for automated testing, building, and deployment of projects whenever changes are pushed to the repository, streamlining the software development lifecycle.

GitHub has gained significant popularity among developers and open-source projects due to its user-friendly interface, collaborative features, and strong community support. It serves as a hub for developers to showcase their work, contribute to open-source projects, and collaborate with other like-minded individuals or teams.

What is Version Control? How many types of version controls do we have?

Version control is a system that allows you to manage and track changes to files or a collection of files over time. It helps developers and teams keep track of different versions of their code, collaborate efficiently, and maintain a history of changes made to a project.

There are three main types of version control systems:

  1. Local Version Control Systems: In a local version control system, such as RCS (Revision Control System), the repository is stored on the local machine. It keeps track of file versions and changes within the same system, typically using a database. While it offers basic versioning capabilities, it lacks collaboration features and doesn't support remote access.

  2. Centralized Version Control Systems (CVCS): Centralized version control systems, such as Subversion (SVN), have a central server that stores the repository. Developers check out files from the central server to work on them and then commit their changes back to the server. These systems offer better collaboration than local systems, allowing multiple developers to work on the same project concurrently. However, they heavily rely on the central server, and if it goes down, it can severely impact collaboration and access to the codebase.

  3. Distributed Version Control Systems (DVCS): Distributed version control systems, like Git, provide a more flexible and decentralized approach to version control. With a DVCS, each developer has their own copy of the entire repository, including the complete history. This allows developers to work independently, make changes offline, and seamlessly merge their changes with others' copies when connected. DVCS systems offer excellent collaboration capabilities, robust branching and merging, and the ability to work with remote repositories. Git has become the most popular and widely adopted DVCS.

Overall, the decentralized nature of a DVCS allows for greater collaboration, flexibility, and security, making it a popular choice for many teams. Each type of version control system has its advantages and disadvantages, and the choice depends on the specific needs and workflows of a development team or individual. However, distributed version control systems like Git have gained significant popularity due to their powerful features, ease of use, and efficient collaboration capabilities.

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

There are several reasons why distributed version control systems (DVCS) like Git are preferred over centralized version control systems (CVCS) like Subversion (SVN). Here are some key advantages of using DVCS:

  1. Offline Work: With a DVCS, developers have a complete copy of the entire repository, including the history, on their local machines. This allows them to work offline and make commits, create branches, and explore the codebase without needing a connection to a central server. Offline work is beneficial in situations where internet access is limited or unreliable, such as during travel or in remote locations.

  2. Flexibility and Speed: DVCS systems provide greater flexibility and speed in various aspects of version control. Since every developer has a local copy of the repository, operations like committing changes, creating branches, or viewing history are faster because they do not require network communication with a central server. Developers can switch between branches, experiment with different ideas, and merge changes locally before pushing them to the remote repository.

  3. Branching and Merging: DVCS systems excel in branching and merging workflows. Branching is lightweight and quick, allowing developers to create and switch between branches effortlessly. Each developer can work on their own branch independently without affecting the main codebase. Merging branches in a DVCS is generally easier and more flexible, providing options for different merging strategies and better conflict resolution capabilities.

  4. Collaboration and Code Review: DVCS enhances collaboration by enabling developers to share their work with others through branching, merging, and pull requests. Each developer can maintain their own branch, work on features or bug fixes, and propose changes to be merged into the main codebase. This promotes a more flexible and decentralized development environment where collaboration is not hindered by a central server's availability or performance.

  5. Redundancy and Backup: In a DVCS, every developer has a complete copy of the repository. This redundancy provides additional safeguards against data loss or server failures. If a central server goes down, developers can continue working with their local copies and push changes once the server is back online. This distributed nature ensures that multiple copies of the codebase exist, acting as backups and reducing the risk of losing work.

  6. Open Source Community: DVCS systems like Git have gained significant popularity in the open-source community. Many high-profile open-source projects use Git and are hosted on platforms like GitHub. This widespread adoption has created a large and active community of developers who contribute, collaborate, and share code using DVCS, making it easier for others to join and participate in open-source projects.

While centralized version control systems still have their use cases and benefits, the advantages offered by distributed version control systems have made them the preferred choice for many development teams and individual developers.

Git Installation

Git is a tool for version control that is not pre-installed. To install follow the below steps,

For RHEL,

$ sudo yum install git -y

For Ubuntu,

$ sudo apt-get install git -y

If its a window system or you want to download it then- You can download it from the official website at https://git-scm.com/downloads

  • Create a free account on GitHub (if you don't already have one). You can sign up at https://github.com/

Git basic commands

Initializing a repository $ git init

Staging files $git add . where after add you can mention the file name or . for all the changed files.

Create a new repository on the command line

$ echo "# git-test-repo" >> README.md
$ git init
$ git add README.md
$ git commit -m "first commit"
$ git branch -M main
$ git remote add origin https://github.com/patelnildip/git-test-repo.git
$ git push -u origin main

To clone the Git repo

$ git clone <repo url>
$ git clone https://github.com/patelnildip/git-test-repo.git

Push an existing repository from the command line

$ git remote add origin https://github.com/patelnildip/git-test-repo.git
$ git branch -M main
$ git push -u origin main

How to configure a token to avoid entering credentials?

Login to GitHub,

\>Go to profile settings

\> Go to "Developer Options"

\> Click on "Personal access tokens"

\>Select "Generate new token"

\> Click on "Generate new token (classic)"

Copy token it is looking like this - ghp_y34XW0p13D8kxcPCB2c6YwRkluQFgwok6frG

$ git remote add origin https://ghp_y34XW0p13D8kxcPCB2c6YwRkluQFgwok6frG@github.com/patelnildip/git-test-repo.git
$ git branch -M main
$ git push -u origin main

When you generate the token and then add it to the git then you can easily push the code without asking for credentials.

Thanks for reading the blog!