Deep Dive into Git & GitHub for DevOps Engineers

Nitin DhimanNitin Dhiman
4 min read

Today, we delve deep into Git and GitHub, essential tools for any DevOps engineer. Understanding version control is crucial for managing code efficiently, collaborating with team members, and maintaining a robust development workflow. Let's explore the fundamentals and some advanced concepts to enhance our DevOps toolkit.

What is Git and Why is it Important?

Git is a distributed version control system (DVCS) that helps developers track changes, work together on code, and keep a complete history of a project's progress. Unlike centralized version control systems (CVCS), Git saves the entire repository, including its history, on each developer's computer. This means you can work offline and collaborate more easily.

Why is Git important?

  • Version Tracking: Git tracks changes at a granular level, allowing you to see what changes were made, who made them, and when.

  • Collaboration: Multiple developers can work on the same project simultaneously, merging their changes seamlessly.

  • Branching and Merging: Git's powerful branching and merging capabilities enable isolated development and easy integration of features.

  • Backup and Recovery: With Git, you can revert to previous versions, recover lost code, and maintain a robust backup system.

What is the Difference Between Main Branch and Master Branch?

Historically, the default branch in Git repositories was named "master." However, in recent years, there has been a shift towards using "main" as the default branch name to promote inclusivity and avoid potentially problematic terminology. Both "main" and "master" serve the same purpose: they are the primary branches where the stable, production-ready code resides.

Can You Explain the Difference Between Git and GitHub?

  • Git: A version control system that runs locally on your machine. It helps you track changes, manage code versions, and collaborate with others.

  • GitHub: A web-based platform that hosts Git repositories. It provides additional features like issue tracking, code reviews, and collaboration tools.

How Do You Create a New Repository on GitHub?

Creating a new repository on GitHub is straightforward:

  1. Log in to your GitHub account.

  2. Click the "New" button on the repositories page.

  3. Fill in the repository name, description (optional), and choose whether it will be public or private.

  4. Click "Create repository."

What is the Difference Between Local & Remote Repository? How to Connect Local to Remote?

  • Local Repository: The Git repository on your local machine where you make changes and commits.

  • Remote Repository: The Git repository hosted on platforms like GitHub, which serves as a central location for collaboration.

Connecting Local to Remote:

  1. Create a local repository: git init

  2. Add the remote repository: git remote add origin <URL>

  3. Push changes to the remote: git push -u origin main

Tasks

Task 1: Set Your Username and Email Address

git config --global user.name "Your Name"
git config --global user.email "youremail@example.com"

Task 2: Create a Repository Named "Devops" on GitHub

  1. Create the repository on GitHub.

  2. Clone the repository to your local machine: git clone <URL>

  3. Connect local repository to GitHub remote:

git remote add origin <URL>
git push -u origin main

Task 3: Create a New File in Devops/Git/Day-02.txt & Add Content

Create and add content:

echo "Your content" > Devops/Git/Day-02.txt
git add Devops/Git/Day-02.txt
git commit -m "Add Day-02.txt with initial content"
git push origin main

Advanced Git & GitHub for DevOps Engineers

Git Branching Branches allow you to develop features, fix bugs, or experiment with new ideas without affecting the main codebase. Each repository has one default branch (main or master) and multiple feature branches.

Git Revert and Reset

  • git reset: Undo changes by moving the current branch pointer to a previous commit.

  • git revert: Create a new commit that undoes the changes from a previous commit.

Git Rebase and Merge

  • git rebase: Integrate changes from one branch to another, rewriting the commit history.

  • git merge: Combine changes from different branches, preserving the commit history.

Tasks

Task 1:

  1. Create version01.txt in a new branch:
git checkout -b dev
echo "This is first feature of our application" > Devops/Git/version01.txt
git add Devops/Git/version01.txt
git commit -m "Added new feature"
git push origin dev
  1. Add more content and commit:
echo "This is the bug fix in development branch" >> Devops/Git/version01.txt
git commit -am "Added feature2 in development branch"
git push origin dev

echo "This is gadbad code" >> Devops/Git/version01.txt
git commit -am "Added feature3 in development branch"
git push origin dev

echo "This feature will gadbad everything from now" >> Devops/Git/version01.txt
git commit -am "Added feature4 in development branch"
git push origin dev
  1. Revert to a previous version:
git revert HEAD~2
git push origin dev

Task 2:

  • Demonstrate branches with screenshots.

  • Merge dev branch into main:

bashCopy codegit checkout main
git merge dev
git push origin main
  • Try git rebase:
git checkout dev
git rebase main
git push origin dev

Conclusion

Today's deep dive into Git and GitHub has equipped us with essential skills for version control and collaboration in DevOps. From understanding the basics to exploring advanced features like branching, merging, and rebasing, we've covered a lot of ground. Keep practicing these commands and concepts to solidify your understanding. Stay tuned for more exciting challenges ahead!

0
Subscribe to my newsletter

Read articles from Nitin Dhiman directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Nitin Dhiman
Nitin Dhiman