Day9 of #90daysofdevops | Deep Dive in Git & GitHub for DevOps Engineers

Day9 of #90daysofdevops | Deep Dive in Git & GitHub for DevOps Engineers

Β·

10 min read

What is Git and why is it important?

Git is a distributed version control system (VCS) designed to handle everything from small to very large projects with speed and efficiency. It was created by Linus Torvalds in 2005 to manage the development of the Linux kernel. Git allows multiple developers to collaborate on a project, enabling them to track changes, work on separate parts of the codebase simultaneously, and merge their changes seamlessly.

Why is Git important for DevOps?

  1. Version Control: Git allows developers to keep track of changes made to their codebase over time. This ensures that the history of the project is well-documented, and developers can easily go back to previous versions if needed. This is crucial for troubleshooting issues, identifying bugs, and understanding the evolution of the project.

  2. Collaboration and Teamwork: In DevOps environments, collaboration between developers, operations, and other stakeholders is crucial. Git provides a platform for teams to work together efficiently, as multiple developers can work on different branches of the same project simultaneously. Once they're done with their changes, they can merge them back into the main codebase.

  3. Branching and Merging: Git's ability to create branches allows developers to work on new features or bug fixes without disturbing the main codebase. This way, the main code remains stable, and new changes can be tested independently. When the feature is ready, it can be merged back into the main branch, ensuring a smooth and controlled integration process.

  4. Continuous Integration and Continuous Deployment (CI/CD): Git integrates seamlessly with CI/CD pipelines, which are at the core of DevOps practices. With CI/CD, developers can automate the process of building, testing, and deploying code changes. Git's ability to trigger CI/CD workflows on specific events (e.g., new commits or pull requests) streamlines the development and deployment process.

  5. Code Reviews: Git provides an excellent platform for conducting code reviews. Before merging code changes into the main branch, developers can request feedback from their peers, ensuring code quality, identifying potential issues, and promoting knowledge sharing within the team.

  6. Fault Tolerance and Redundancy: Git is a distributed VCS, meaning each developer has a complete copy of the repository. This provides fault tolerance and redundancy since the codebase is not reliant on a central server. If one repository becomes unavailable, developers can continue working with their local copies and later synchronize with other repositories.

  7. Open Source Ecosystem: Git has a vast open-source ecosystem with various tools and integrations that enhance its functionality. Platforms like GitHub, GitLab, and Bitbucket provide hosting services, issue tracking, and pull request management, making collaboration and project management even more accessible.

In conclusion, Git is a fundamental tool in the DevOps toolkit due to its capabilities in version control, collaboration, branching, merging, and integration with CI/CD pipelines. Its distributed nature and open-source ecosystem make it the preferred choice for managing code in modern software development and DevOps practices.

What is the difference Between Main Branch and Master Branch??

In traditional version control systems, the default and primary branch where all development activities typically take place is named "master." This has been the long-standing convention in many version control tools, including Git.

However, in recent years, the tech industry has recognized that the term "master" may have connotations that are insensitive and offensive due to its historical associations with slavery and discrimination. As a result, many open-source projects and companies have decided to replace "master" with more neutral terms like "main," "mainline," or "default."

So, the main difference between the "main" branch and the "master" branch is essentially just the name. Functionally, they serve the same purpose: to act as the primary branch where the latest stable codebase resides and from which all feature branches are typically created.

It's worth noting that this change in naming convention is not mandatory, and some projects or organizations may still choose to use "master" as their primary branch. However, the trend is shifting towards more inclusive terminology, and "main" has become the more commonly accepted replacement for "master" in recent times.

Can you explain the difference between Git and GitHub?

πŸ™Git:

  1. Git is a distributed version control system designed to manage and track changes to source code and other files.πŸ”„

  2. It was created by Linus Torvalds in 2005 and is widely used by developers to collaborate and work on projects efficiently.πŸ‘¨β€πŸ’»

  3. Git allows developers to create branches, make changes, and merge those changes back into the main codebase.🌳

  4. It works locally on your computer, meaning you can perform version control operations without needing an internet connection.πŸ’»

  5. Git is a command-line tool, although various graphical user interfaces (GUIs) are available to make it more user-friendly.πŸ–₯️

🌐GitHub:

  1. GitHub is a web-based hosting service that provides a platform for Git repositories to be stored and shared. 🌐

  2. It was founded in 2008 and quickly became one of the most popular Git repository hosting platforms. πŸš€

  3. GitHub offers a web interface that simplifies repository management, issue tracking, and collaboration among team members. 🀝

  4. It provides features like pull requests, code reviews, and project management tools to enhance the development workflow. πŸ”„πŸ”πŸ“

  5. GitHub allows developers to work together on projects, contributing to the same codebase, and managing changes effectively. πŸ‘₯πŸ‘₯

In summary, Git is the version control system itself, enabling developers to track changes and manage their code locally. On the other hand, GitHub is a cloud-based platform that hosts Git repositories and offers collaboration and project management features, making it easier for teams to work together on software development projects. Other platforms like GitLab and Bitbucket also provide similar services to host Git repositories, but GitHub is widely recognized and utilized in the open-source community. 🌐

How do you create a new repository on GitHub?

  1. Sign in to your GitHub account: Go to github.com and sign in with your credentials. If you don't have an account, you'll need to create one first.

  2. Once you're logged in, click on the "+" sign in the top right corner of the GitHub website. From the dropdown menu, select "New repository."

  3. On the "Create a new repository" page, you'll be prompted to enter the following information:

    • Repository name: Choose a name for your new repository. Make it descriptive but concise.

    • Description: Optional, but it's a good idea to provide a brief description of your repository's purpose.

    • Visibility: Choose whether your repository will be public (visible to everyone) or private (accessible only to you and the collaborators you invite).

    • Initialize this repository with a README: If you check this option, GitHub will create an initial README file for your repository. It's often a good idea to have a README to explain what your project is about.

    • .gitignore: Optionally, you can choose a template for the .gitignore file to exclude certain files from being tracked by Git.

    • License: Optionally, you can choose an open-source license for your project.

  4. Once you've filled in the desired information, click on the green "Create repository" button at the bottom of the page.

Congratulations! You have now created a new repository on GitHub.

What is the difference between local & remote repositories? How to connect local to remote?

Local Repository:

  1. A local repository is the version of the project that resides on your computer's hard drive.

  2. It contains all the files, commits history, and branches related to your project.

  3. The local repository is primarily used for development and making changes to the codebase.

  4. It allows you to work on your project even when you are not connected to the internet.

  5. Common Git commands like commit, branch, merge, and log are executed on the local repository.

Remote Repository:

  1. A remote repository is located on a server or a remote hosting service like GitHub, GitLab, or Bitbucket.

  2. Its primary purpose is to serve as a central hub for collaboration and to share your project with others.

  3. Remote repositories facilitate teamwork, allowing multiple developers to access, contribute, and synchronize their changes to the codebase.

  4. It acts as a backup of your project, protecting against data loss from local hardware failures.

  5. Remote repositories are accessed over the internet, allowing developers to work together from different locations.

Connecting Local to Remote Repository: To connect your local repository to a remote repository, you typically follow these steps:

  1. Create a Remote Repository:

    • Sign in to your account on a remote hosting service like GitHub, GitLab, or Bitbucket.

    • Create a new repository on the platform with an appropriate name and optional description.

  2. Add the Remote URL to Your Local Repository:

    • On your local machine, navigate to your project's directory using the command line (Terminal, Command Prompt, or Git Bash).

    • Use the following command to add the remote repository's URL as the origin (you can replace "URL" with the actual repository URL):

        git remote add origin URL
      
  3. Push Your Local Repository to the Remote:

    • Once the remote is added, you can push your local codebase to the remote using the following command:

        git push -u origin master
      
    • This command pushes the contents of your local "master" branch to the "master" branch of the remote repository. If you are working on a different branch, replace "master" with the branch name.

After completing these steps, your local and remote repositories will be connected. You can now push your changes to the remote repository using git push and pull changes from the remote repository using git pull. Additionally, you can collaborate with other developers by inviting them as collaborators to the remote repository.

Tasks:

(A)Set your user name and email address, which will be associated with your commits.

To set your user name and email address, you need to use the Git configuration commands. Open a terminal or command prompt and execute the following commands, replacing "Your Name" and "" with your desired name and email address:

  1. Set your Git user name:
git config --global user.name "Your Name"
  1. Set your Git email address:
git config --global user.email "your.email@example.com"

The --global flag ensures that this configuration is applied globally, meaning it will be used for all your Git repositories on your computer. If you want to set the user name and email address only for a specific repository, you can remove the --global flag and run the commands within the repository's directory.

Once you've set your user name and email address, every commit you make with Git will include this information, making it easier to track who made each change to the codebase. It is essential to configure this information correctly, especially when collaborating with others on projects, as it helps maintain a clear history of contributions.

(B)
Create a repository named "Devops" on GitHub
Connect your local repository to the repository on GitHub.
Create a new file in Devops/Git/Day-02.txt & add some content to it
Push your local commits to the repository on GitHub

Create a Repository on GitHub:

  • Go to github.com and sign in to your GitHub account.

  • Click on the "+" sign in the top right corner and choose "New repository."

  • Name the repository "Devops" and provide an optional description.

  • Choose the repository visibility (public or private) as per your preference.

  • Optionally, you can initialize the repository with a README, add a .gitignore, and choose a license.

Click on "Create repository" to create the "DevOps" repository on GitHub.

Connect Local Repository to GitHub Repository:

  • Open your terminal or command prompt and navigate to your local "Devops" project directory.

  • Use the following command to add the remote URL of the GitHub repository as the origin:

      git remote add origin <GitHub_Repository_URL>
    

    Replace <GitHub_Repository_URL> with the URL of your "Devops" repository on GitHub. It will look something like: https://github.com/your-username/Devops.git

Create a New File and Add Content:

  • In your local "Devops" project directory, navigate to the "Git" directory (if it doesn't exist, create it) using the terminal or command prompt.

  • Create a new file named "Day-02.txt" and add some content to it using your preferred text editor.

Commit and Push Changes to GitHub:

  • In the terminal or command prompt, add the new file to the Git staging area using the following command:

      git add Git/Day-02.txt
    
  • Commit the changes with a descriptive message:

      git commit -m "Add Day-02.txt with content"
    
  • Finally, push the local commits to the GitHub repository's "master" branch (assuming you are on the "master" branch) using:

After following these steps, your local "Day-02.txt" file with its content will be added to the "DevOps" repository on GitHub.

Thanks for reading the blog & do share them with someone in need :)

Please share your views and suggestions, they are always welcome.

See you then in the next blog.

Happy learning :)

Β