๐Ÿš€ Unlocking the Power of Git and GitHub: A DevOps Engineer's Journey (Part 2) ๐Ÿš€

Advance Git & GitHub for DevOps Engineers: Part-2: Day 11 #90DaysofDevops

ยท

10 min read

๐Ÿš€ Unlocking the Power of Git and GitHub: A DevOps Engineer's Journey (Part 2) ๐Ÿš€

Table of contents

Hey there, fellow developers! ๐Ÿ‘‹ Have you ever found yourself juggling multiple tasks, working on different branches, and hesitant to commit changes in your current branch? Trust me; I've been there too! But worry not, for I'm here to share my personal experience and insights on how I mastered Git Stash, Git Cherry-pick, and Conflict Resolution, making my life as a DevOps engineer so much smoother and efficient! ๐Ÿ†

In this blog post, I'll take you on a thrilling ride through my Git and GitHub journey, highlighting the power of Git Stash, the flexibility of Git Cherry-pick, and the art of resolving conflicts like a pro! ๐Ÿ’ก So, fasten your seatbelts and let's dive into this adventure together! ๐ŸŒŠ

๐Ÿท๏ธ Git Stash: Saving Changes Temporarily

Imagine this scenario: You're working on a feature in your current branch when suddenly, a critical bug pops up that demands immediate attention. You need to switch to a different branch urgently, but you don't want to lose the changes you've made in your current branch. That's where Git Stash comes to the rescue! ๐Ÿฆธโ€โ™‚๏ธ

What is Git Stash?

Git Stash is a nifty command that allows you to temporarily save changes in your working directory without committing them. This is incredibly useful when you need to switch to a different branch to work on something else urgently, but you don't want to commit your changes just yet. Instead, you stash them away safely, so you can pick them up later when it's convenient for you. ๐Ÿ—„๏ธ

Using Git Stash

To utilize Git Stash effectively, you'll first create a new branch and make your desired changes. Then, with a simple git stash command, you can save those changes in a new stash. This action removes the changes from your working directory and records them safely in the stash. When the time is right, you can apply these changes with ease. ๐Ÿ“

Retrieving Stashed Changes

Life gets busy, and sometimes we forget about the stashed changes we've made. Fear not! You can easily retrieve them when needed using the git stash list command, which displays the list of stashed changes. Pick and apply the one that suits your requirements. ๐Ÿ“š

Managing Stashes

As you continue working on various tasks, you may accumulate several stashes. Don't worry; Git has got your back! You can delete individual stashes using git stash drop, or clear all stashes with git stash clear. This way, your stash collection stays organized and clutter-free! ๐Ÿ—‚๏ธ

๐Ÿ’ Git Cherry-pick: Selective Commit Application

Picture this: You've been working on multiple branches, and now you need to cherry-pick specific commits from one branch and apply them to another. How do you do that without bringing over the entire branch? That's where Git Cherry-pick comes into play! ๐Ÿ’

Understanding Git Cherry-pick

Git Cherry-pick is a powerful command that enables you to select specific commits from one branch and apply them to another. This ability to cherry-pick only the changes you need can save you time and effort, especially when working on projects with multiple branches. ๐ŸŒฑ

Cherry-picking Commits

To leverage Git Cherry-pick effectively, you'll create two new branches and make commits in each. Then, using the git cherry-pick <commit_hash> command, you can choose the specific commits from one branch and transplant them onto the other. This selective application of changes is a game-changer! ๐ŸŽฎ

Selectively Applying Changes

There are many situations where Git Cherry-pick proves invaluable. Whether it's backporting bug fixes, applying specific features to different branches, or even merging independent changes, Git Cherry-pick has your back! ๐Ÿ’

Potential Pitfalls

As with any powerful tool, there are potential pitfalls to watch out for. But don't worry, I've got you covered! I'll share common mistakes and how to avoid them, ensuring smooth sailing as you cherry-pick your way through Git's vast ocean! โ›ต

๐Ÿคผโ€โ™‚๏ธ Resolving Conflicts: Navigating Through Rough Waters

Ah, conflictsโ€”the bane of every developer's existence. Merging or rebasing branches can sometimes result in conflicts that demand manual resolution. But fear not! Armed with the right knowledge, you can tackle these challenges head-on! ๐Ÿ›ก๏ธ

Understanding Conflicts

Conflicts arise when you merge or rebase branches that have diverged, and Git is unsure of which changes to apply. This can lead to conflicts that need to be resolved manually before Git can proceed with the merge or rebase. But fear not! I'll guide you through the process like a seasoned captain navigating rough waters. ๐Ÿšข

Identifying Conflicts

When you encounter conflicts, Git has your back by displaying a list of files with conflicts using the git status command. This makes it easier to pinpoint the affected files and areas that need your attention. ๐Ÿงญ

Resolving Conflicts

The process of resolving conflicts can feel overwhelming at first, but I'll provide a detailed, step-by-step guide to help you tackle them like a pro. With patience and practice, you'll be confidently resolving conflicts in no time! ๐Ÿ’ช

Adding Resolved Files

After successfully resolving conflicts, it's essential to use the git add command to finalize the conflict resolution. This tells Git that you've addressed the conflicts and are ready to proceed with the merge or rebase. ๐Ÿ

Task-01: A Step-by-Step Guide to Git Branching and Using "git stash"

Introduction to Git Branching

Git is a powerful version control system that allows developers to work collaboratively and efficiently. One of its essential features is branching, which enables developers to create separate lines of development without affecting the main project. This article will guide you through the process of creating a new branch, making changes, and using "git stash" effectively.

Creating a New Branch

Branching in Git allows developers to work on different features or bug fixes without interfering with the main codebase. To create a new branch, follow these steps:

  1. Open your terminal or command prompt.

  2. Navigate to the root directory of your project using the cd command.

  3. Use the following command to create a new branch:

git checkout -b new-feature

In this example, we're creating a new branch called "new-feature." You can name your branch according to the specific feature or issue you are working on.

Making Changes and Using "git stash"

Now that you have created a new branch, you can start making changes to your code. However, what if you need to switch to another branch to fix a critical bug? Using "git stash" allows you to save your changes temporarily without committing them. Here's how:

  1. After making changes to your code, execute the following command:
git stash save "Work in progress"

This command will stash your changes, and the message "Work in progress" will help you identify the changes later.

Switching to a Different Branch

To switch to a different branch in Git, follow these steps:

  1. Ensure that your current working directory is clean (no pending changes).

  2. Use the following command to switch to the desired branch:

git checkout another-branch

Replace "another-branch" with the name of the branch you want to switch to.

Committing Changes

Now that you are on a different branch and have made the necessary changes, it's time to commit them. Use the following steps:

  1. Add the changes to the staging area:
git add .

The dot "." signifies that all changes in the current directory will be added.

  1. Commit the changes with a descriptive message:
git commit -m "Fix critical bug"

Using "git stash pop"

Once you have committed your changes on a different branch, you may need to bring back the changes you stashed earlier. The "git stash pop" command helps you apply the changes on top of your new commits. Follow these steps:

  1. Ensure you are on the branch where you stashed the changes (in this case, "new-feature").

  2. Execute the following command:

git stash pop

The changes you stashed earlier will be applied to your current branch.

Task-02: Adding and Reflecting Feature Commits in Production Branch

In this task, we will add new features to the "development" branch and ensure that the commit messages are reflected in the "Production" branch, which will be branched out from the "Master" branch. We will achieve this by using the "rebase" command.

Step 1: Adding the New Features in "development" Branch

First, let's open the file "version01.txt" in the "development" branch and add the new lines as instructed:

version01.txt

This is the bug fix in the development branch
Line2>> After bug fixing, this is the new feature with minor alterationโ€
Line3>> This is the advancement of the previous feature
Line4>> Feature 2 is completed and ready for release

Step 2: Committing the New Features

We will now commit each new feature separately with appropriate commit messages:

  1. Commit 1: "Added feature2.1 in development branch"

     git add version01.txt
     git commit -m "Added feature2.1 in development branch"
    
  2. Commit 2: "Added feature2.2 in development branch"

     git add version01.txt
     git commit -m "Added feature2.2 in development branch"
    
  3. Commit 3: "Feature2 completed"

     git add version01.txt
     git commit -m "Feature2 completed"
    

Step 3: Creating the "Production" Branch from "Master"

Now, we need to create the "Production" branch from the "Master" branch:

git checkout master  # Switch to the "Master" branch
git pull origin master  # Ensure the "Master" branch is up to date
git checkout -b production  # Create and switch to the "Production" branch

Step 4: Rebasing to Reflect Commit Messages

Next, we will use the "rebase" command to reflect the commit messages from the "development" branch into the "Production" branch:

git rebase development

Step 5: Pushing Changes to Remote Repository

Finally, we will push the changes made in the "Production" branch to the remote repository:

git push origin production

Now, the "Production" branch will have the new features added in the "development" branch along with their respective commit messages. This ensures that both branches are in sync and the new features are ready for deployment.

Task-03: Cherry-Picking and Optimizing a Feature in the Production Branch

In this task, we will cherry-pick the commit "Added feature2.2 in development branch" from the "development" branch and add additional lines to optimize the feature. We will perform these actions in the "Production" branch.

Step 1: Cherry-Picking the Commit

Let's start by switching to the "Production" branch and cherry-picking the commit "Added feature2.2 in development branch":

git checkout production  # Switch to the "Production" branch
git cherry-pick <commit-hash>

Replace <commit-hash> with the hash of the "Added feature2.2 in development branch" commit.

Step 2: Adding Optimizations

After cherry-picking the commit, let's open the file "version01.txt" and add the following lines after "Line3>> This is the advancement of the previous feature":

version01.txt

Line to be added after Line3>> This is the advancement of the previous feature
Line4>> Added few more changes to make it more optimized.

Step 3: Committing the Optimizations

Now, we will commit the optimizations with an appropriate commit message:

git add version01.txt
git commit -m "Optimized the feature"

Step 4: Pushing Changes to Remote Repository

Finally, let's push the changes made in the "Production" branch to the remote repository:

git push origin production

Now, the "Production" branch has the cherry-picked commit "Added feature2.2 in development branch" along with additional lines that optimize the feature. These changes are ready for deployment and will be included in the next release.

In summary, by following the steps mentioned above, we have successfully cherry-picked a commit from the "development" branch and added optimizations to the feature in the "Production" branch. This process ensures that the feature is up-to-date and optimized before being released to users.

๐Ÿค” Frequently Asked Questions (FAQs)

Q: Can I use Git Stash on multiple branches simultaneously?

A: Absolutely! Git Stash works independently on each branch, allowing you to save changes on one branch while working on another.

Q: Is it possible to cherry-pick multiple commits at once?

A: Yes, you can cherry-pick multiple commits by specifying multiple commit hashes with the git cherry-pick command.

Q: What should I do if I accidentally deleted a stash?

A: Don't worry! If you accidentally deleted a stash using git stash drop, you may still recover it through the reflog.

Q: Can Git Cherry-pick lead to conflicts?

A: Yes, Git Cherry-pick can cause conflicts if the selected commits modify the same lines as other commits on the destination branch.

Q: How can I prevent conflicts during merges?

A: Keeping your branches up-to-date and communicating effectively with your team can minimize conflicts during merges.

๐ŸŽ‰ Conclusion

And that concludes our thrilling journey through Git Stash, Git Cherry-pick, and Conflict Resolution! ๐ŸŽŠ By mastering these essential techniques, you'll be well-equipped to handle various version control challenges, ensuring a seamless development experience as a DevOps engineer.

Remember, every developer faces their fair share of challenges, but with the right tools and knowledge, you can navigate through them like a pro! So, embrace the power of Git and GitHub, and let it elevate your development process to new heights! ๐Ÿš€

\============================================

ย