Advancing Git and GitHub for DevOps Engineers: Part 2

Advancing Git and GitHub for DevOps Engineers: Part 2

ยท

4 min read

Introduction

Welcome back to the #90DaysOfDevOps Challenge! Today, we're diving even deeper into the world of Git and GitHub, uncovering more tools and techniques that every DevOps engineer should have in their toolkit. In this second instalment of "Advance Git & GitHub for DevOps Engineers," we'll explore Git Stash, Cherry-pick, and resolving conflicts โ€“ essential skills for mastering version control and collaboration.

Git Stash: Temporarily Tucking Away Changes

Picture this scenario: you're in the middle of coding brilliance, but suddenly need to switch branches. Enter Git stash! This nifty command lets you save your current changes without committing them. Perfect for those "I'll come back to this later" moments. Stashed changes can be retrieved whenever you're ready.

Cherry-pick: Selective Commit Magic

Ever wished you could pluck specific commits from one branch and apply them to another? Git cherry-pick makes it happen! When you want to transplant specific changes, this command is your go-to. No more applying entire branches โ€“ cherry-pick allows you to keep your codebase lean and focused.

Resolving Conflicts: Tackling the Divergence

In the world of merging and rebasing, conflicts can rear their heads. Fear not! Git equips you to tackle them head-on. The git status command flags conflicting files, while git diff shows the differences. With git add, you can resolve the discrepancies and keep your codebase harmonious.

Task 01: Stashing, Popping, and Layering Changes

Step 1: Craft Changes in a New Branch

  1. Open your terminal and navigate to your repository directory.

  2. Create a new branch named "feature-branch" and switch to it:

  3. Create a new text file named feature.txt and add some content to it:

  4. Stage and commit the changes:

Step 2: Temporarily Stash the Changes

  1. While still on the "feature-branch," stash your changes:

Step 3: Switch Branches and Make New Commits

  1. Switch to a different branch, such as "main":

  2. Create a new text file named bug-fix.txt and add some content to it:

  3. Stage and commit the changes:

Step 4: Pop the Stashed Changes

  1. Return to the "feature-branch" where you stashed changes:

  2. Apply the stashed changes on top of the new commits:

  3. You'll notice that the content from feature.txt is back in your working directory.

Here's a summary of the commands you've used:

# Step 1
git checkout -b feature-branch
# Create and edit feature.txt
git add feature.txt
git commit -m "Added a new feature in feature-branch"

# Step 2
git stash

# Step 3
git checkout main
# Create and edit bug-fix.txt
git add bug-fix.txt
git commit -m "Fixed a bug in the main branch"

# Step 4
git checkout feature-branch
git stash pop

Task 02: Cherry-picking and Rebase Exploration

  1. Enhance the version01.txt file with meaningful content.

  2. Commit the changes with specific messages.

  3. Ensure the commit messages flow seamlessly into the Production branch, stemming from the Master branch via rebase.

    Here's a summary of the commands you've used:

     # Step 1: Enhance version01.txt
     # Edit version01.txt with meaningful content
     This is the bug fix in development branch.
     After bug fixing, this is the new feature with minor alteration.
    
     # Step 2: Commit the Changes
     git add version01.txt
     git commit -m "Enhanced version01.txt with additional content"
    
     # Step 3: Rebase and Ensure Seamless Commit Flow
     git checkout main
     git rebase feature-branch
     # Resolve conflicts if needed
    

Task 03: Cherry-picking for Fine-tuning

  1. Cherry-pick a commit from a different branch.

  2. Apply further changes to the cherry-picked commit.

  3. Commit the refined changes with an optimized twist.

     # Step 1: Cherry-pick a Commit
     git checkout main
     git cherry-pick 8985f05
    
     # Step 2: Apply Further Changes to the Cherry-picked Commit
     # Edit version01.txt with additional content
    
     # Step 3: Commit the Refined Changes
     git add version01.txt
     git commit -m "Optimized the feature"
    

Embrace the Challenge and Share the Knowledge

Version control mastery is a pivotal DevOps skill. Embrace the tasks, immerse yourself in the intricacies of Git stash, cherry-pick, and conflict resolution. . Let's learn and grow together in this exciting DevOps journey! ๐Ÿš€๐Ÿ‘ฉโ€๐Ÿ’ป #GitPro #DevOpsAdvancement

Happy Learning!!!

Reference

To develop deeper into the world of DevOps I highly recommend following Shubham Londhe on TrainWithShubham and Bhupinder Rajput on Technical Guftgu.

ย