Fundamentals of Git & GitHub- Version Control System. #Day8 #90daysofdevops

Β·

8 min read

Fundamentals of Git & GitHub- Version Control System. #Day8 #90daysofdevops

πŸ“ Introduction

Hey there, DevOps enthusiasts! Today, we're diving into the world of version control, Git, and GitHub. As a DevOps engineer, I can't stress enough how essential these tools are in modern software development. They enable collaboration, version tracking, and streamlined workflows. Let's explore what Git is, what GitHub does, and why version control is crucial for every development team.

πŸ’» What is Git?

Git, created by Linus Torvalds in 2005, is a distributed version control system that allows you to track changes to files and coordinate work on those files among multiple people. It is commonly used for software development, but it can be used to track changes to any set of files.

🌐 What is GitHub?

GitHub, on the other hand, is a web-based platform that hosts Git repositories. It provides a user-friendly interface to manage and collaborate on Git repositories. GitHub offers various features such as issue tracking, pull requests, and continuous integration, making it a powerful platform for developers to work together seamlessly.

πŸ’‘ What is Version Control?

Version control is a system that tracks changes to a file or set of files over time so that you can recall specific versions later. It allows you to revert files to a previous state, revert the entire project to a previous state, and compare changes over time. It allows multiple team members to work on the same project simultaneously without stepping on each other's toes. Version control ensures that every change, addition, or deletion to the codebase is carefully recorded, creating a history that can be referenced, reverted, or merged as needed.

πŸ“Œ Types of Version Control

There are two primary types of version control systems:

  1. Centralized Version Control: In a centralized version control system (e.g., Subversion and Perforce), there is a central server that stores the entire codebase and its history. Developers work on their local copies and then commit changes to the central repository. While this system facilitates collaboration, it has some drawbacks. For instance, if the central server goes down, it becomes impossible to access the repository or commit changes.

  2. Distributed Version Control: A distributed version control system (DVCS) allows developers to "clone" an entire repository, including the entire version history of the project. This means that they have a complete local copy of the repository, including all branches and past versions. Developers can work independently and then later merge their changes back into the main repository. Examples of DVCS include Git, Mercurial, and Darcs.

This approach brings many advantages, including:

πŸš€ Why use Distributed Version Control (Git) over Centralized Version Control?

  1. Better collaboration: In a DVCS, every developer has a full copy of the repository, including the entire history of all changes. This makes it easier for developers to work together, as they don't have to constantly communicate with a central server to commit their changes or to see the changes made by others.

  2. Improved speed: Because developers have a local copy of the repository, they can commit their changes and perform other version control actions faster, as they don't have to communicate with a central server.

  3. Greater flexibility: With a DVCS, developers can work offline and commit their changes later when they do have an internet connection. They can also choose to share their changes with only a subset of the team, rather than pushing all of their changes to a central server.

  4. Enhanced security: In a DVCS, the repository history is stored on multiple servers and computers, which makes it more resistant to data loss. If the central server in a CVCS goes down or the repository becomes corrupted, it can be difficult to recover the lost data.

    Overall, the decentralized nature of a DVCS allows for greater collaboration, flexibility, and security, making it a popular choice for many teams.

What is branch and root in Git?

🌿 Branch:

  • In Git, a branch is a parallel version of the codebase that allows developers to work on different features or bug fixes independently.

  • Think of a branch as a separate timeline or pathway within the project's history.

  • Each branch has its own set of commits, which record changes made exclusively on that branch.

  • Branches are lightweight and easy to create, making it convenient to experiment and work on new features without affecting the main codebase.

  • Developers can switch between branches to work on different tasks or merge changes from one branch into another when ready.

🌱 Root:

  • The "root" in Git refers to the initial commit of a repository, also known as the "first commit."

  • It is the starting point of the version control history for the project.

  • The root commit has no parent commit, as it marks the beginning of the codebase and has no previous history to reference.

  • As more commits are made, the Git history forms a tree-like structure, with the root commit at the base and subsequent commits branching out from it.

πŸ’‘ Fun Fact: Just like the root of a tree provides the foundation for all its branches and leaves, the Git "root" commit is the foundation for the entire version control history of the project! 🌳🌱

Remember, branches are powerful tools that enable collaboration and development flexibility, while the root commit serves as the starting point of your project's journey.

In Git and GitHub, "pull," "fetch," and "push" are three essential commands used to interact with remote repositories. Each command serves a distinct purpose in managing code synchronization and collaboration among team members. Let's explore the differences between these commands:

What is the difference between Pull, Fetch & Push in Git & Github?

  1. Pull:

    The "pull" command is used to fetch and automatically merge changes from a remote repository into your current local branch. It is a combination of two actions: "fetch" and "merge." When you pull, Git will retrieve any new commits from the remote repository and then automatically merge them into your local branch. If there are any conflicts between your local changes and the remote changes, you'll need to resolve them before the merge is successful.

Usage:

git pull <remote> <branch>

Example:

git pull origin main
# origin is github.
# main is github master repository.
  1. Fetch:

    The "fetch" command is used to retrieve changes from a remote repository without automatically merging them into your local branch. It only downloads the changes and updates the remote-tracking branches (e.g., origin/main), allowing you to see what other team members have been working on. Unlike "pull," "fetch" will not modify your working directory, so it's safe to use when you want to inspect changes before merging them.

Usage:

git fetch <remote>

Example:

git fetch origin

# origin is github.
  1. Push: The "push" command is used to upload your local commits to a remote repository. After working on your local branch and committing changes, you can use "push" to share those commits with your team members on the shared remote repository (like GitHub). It is essential to pull or fetch and merge any remote changes before pushing your changes to avoid conflicts.

Usage:

git push <remote> <branch>

Example:

git push origin main
# origin is github.
# main is github master repository.

In summary, "pull" is used to fetch and merge remote changes into your local branch, "fetch" is used to retrieve changes without merging, and "push" is used to upload your local commits to a remote repository. These commands are crucial for efficient collaboration and code synchronization in distributed version control systems like Git, especially when using platforms like GitHub for team collaboration.

πŸ”§ Common Git Commands:

Here are some commonly used Git commands to help you get started:

  1. git init: Initializes a new Git repository in your project folder.

  2. git add <filename>: Adds a file to the staging area, ready to be committed.

  3. git commit -m "commit message": Commits the changes in the staging area to the repository.

  4. git rm --cached <filename>: To unstaged the file from stagging area.

  5. git restore <filename>: This command use to restoring files in the working tree from either the index or another commit.

  6. git status: This shows the status of your working directory and staging area.

  7. git log: Displays the commit history.

  8. git log --oneline: The main purpose of this command is to print a single commit in a single line as output when the git log command is executed using this option. The first seven characters of the SHA

  9. git log --pretty: It allows you to specify all the fields you want in the output.

  10. git branch: Lists all branches in the repository.

  11. git checkout -b <branch-name>: Creates a new branch and switches to it.

  12. git pull: Fetches and merges change from a remote repository to your local branch.

  13. git push: Pushes your local commits to a remote repository.

  14. git clone <repo> <directory>: Clone the repository located at <repo> into the folder called ~<directory>! on the local machine.

πŸ”š Conclusion:

In conclusion, version control is the backbone of modern software development, and Git is undoubtedly one of the most powerful version control systems out there. By allowing multiple developers to work collaboratively, track changes, and manage code history efficiently, Git enhances productivity and reduces the risk of errors.

Moreover, GitHub provides an excellent platform for hosting Git repositories, facilitating teamwork, code reviews, and continuous integration. Together, Git and GitHub empower development teams to deliver high-quality software faster and with greater confidence.

That's All from My Side!

#Learning in the tech world is a continuous journey.

#trainwithshubham #Devopsjourney #Git #Github #90daysofdevops #versioncontrol

#Devops #Learningdevops.

Β