Day 11 of 90 Days of DevOps Challenge: Advanced Git & GitHub for DevOps Engineers - Part 2 Introduction

As DevOps engineers, mastering Git and GitHub is crucial for managing code efficiently and ensuring smooth collaboration across teams. On Day 11 of the DevOps challenge, we dive deeper into advanced Git commands like git stash, git cherry-pick, and conflict resolution. These commands are essential for handling complex workflows, especially when working on multiple features or hotfixes simultaneously.

Let's break down today's tasks and understand how to use these commands effectively.

Task 01: Using Git Stash for Managing Temporary Changes

Scenario: You're working on a feature in a new branch and need to switch to another branch without committing your current changes. This is where git stash comes into play.

  1. Create a New Branch and Make Changes: Start by creating a new branch:

     git checkout -b new-feature-branch
    
  2. Make some changes to your files. These changes are now in your working directory.

  3. Stash the Changes: Use git stash to save your changes temporarily:

     git stash
    

    This command will remove the changes from your working directory and save them in a stash, allowing you to switch branches without losing progress.

  4. Switch to a Different Branch:Switch to another branch where you need to work:

     git checkout another-branch
    
  5. Make some changes and commit them as usual.

  6. Pop the Stash: Once you're done, return to your original branch and apply the stashed changes on top of the new commits:

     git checkout new-feature-branch git stash pop
    

Key Takeaways: git stash is a powerful tool when you need to pause work on one task and quickly switch to another. The git stash pop command not only restores your changes but also removes them from the stash, helping you continue where you left off.

Task 02: Rebase Commits Across Branches

Scenario: You're adding new features in the development branch and want those commits to be reflected in the production branch, which branches out from master.

  1. Adding Features in Development Branch: Modify version01.txt in the development branch and add the following lines:

    Line2>> After bug fixing, this is the new feature with minor alteration

  2. Commit the change:

     git commit -am "Added feature2.1 in development branch"
    
  3. Add another line:

    Line3>> This is the advancement of the previous feature

  4. Commit the change:

     git commit -am "Added feature2.2 in development branch"
    
  5. Finally, add the last line:

    Line4>> Feature 2 is completed and ready for release

  6. Commit the change:

     git commit -am "Feature2 completed"
    
  7. Rebase to Production Branch:

    Switch to the production branch, which is branched from master:

     git checkout production
    
  8. Rebase the development branch onto the production branch:

     git rebase development
    

    This will replay the commits from development onto production, making the changes available in both branches.

Key Takeaways: Rebasing is an effective way to apply changes from one branch to another, ensuring that the commits maintain a clean and linear history. It’s particularly useful for keeping production up-to-date with the latest developments.

Task 03: Cherry-picking Specific Commits

Scenario: You need to selectively apply a specific commit from the development branch to the production branch and make further optimizations.

  1. Switch to the Production Branch: Checkout the production branch:

     git checkout production
    
  2. Cherry-pick the Commit:

    Identify the commit hash for "Added feature2.2 in development branch" from the development branch. Use the following command to apply it:

     git cherry-pick <commit_hash>
    
  3. Optimize the Feature: Add additional optimizations to the file:

    Line4>>Added a few more changes to make it more optimized.

  4. Commit the changes:

     git commit -am "Optimized the feature"
    

Key Takeaways: git cherry-pick is perfect for when you want to incorporate specific changes from one branch into another without merging the entire branch. This is particularly useful when only certain features or bug fixes are needed in a different branch.


Conclusion

Day 11's tasks focused on advanced Git techniques that are indispensable for DevOps engineers. Whether you're using git stash to manage temporary changes, git cherry-pick to selectively apply commits, or rebasing to keep branches in sync, these tools will help you maintain a clean and efficient codebase. Mastering these commands will significantly improve your ability to manage complex workflows and collaborate effectively within your team.

Stay tuned for more advanced Git and GitHub techniques as we continue the DevOps challenge!