#Day11:- 90 Days DevOps Challenge@Advance Git & GitHub for DevOps Engineers: Part-2

#Day11:- 90 Days DevOps Challenge@Advance Git & GitHub for DevOps Engineers: Part-2

Mastering Version Control and Collaborative Workflows for Seamless DevOps Integration

💫Git Stash:

(👨‍💻👨‍💻👨‍💻Save Your Changes for Later! 🗄️)

Imagine you are working on a project with many changes in your code, but you're not ready to commit those changes yet because they're not complete or might break something. 😱 However, you need to switch to a different branch or work on another task urgently. 🔄

This is where git stash comes in handy. It allows you to temporarily save your changes without committing them. Think of it like putting your changes in a "stash" or a secret drawer so that you can come back to them later. 🤫

Here's how it works in easy steps:

  1. Save your changes: When you want to switch to a different branch or work on something else, simply run git stash. This will save your current changes in a special hidden area. 📥

  2. Switch branches: Now, you can freely switch to a different branch using git checkout without any worries about conflicting changes. 🌳

  3. Do what you need: Work on the new branch or task as needed, without your previous changes getting in the way. 🛠️

  4. Retrieve your changes: When you're ready to continue with the changes you stashed earlier, use git stash pop or git stash apply. git stash pop will apply the changes and remove them from the stash, while git stash apply will apply the changes but keep them in the stash in case you need them again. 🔄

  5. Commit or stash again: After applying the changes, you can choose to commit them as usual or stash them again if you still need to work on them later. 📝

So, git stash helps you save your work temporarily, switch to a different context, and then come back to your previous changes whenever you're ready. It's like a "magic drawer" for your code changes! 🪄🎈

🍒Cherry-pick:

(Selective Commit Magic! ✨)

Git cherry-pick is a command that allows you to select specific commits from one branch and apply them to another. 🍒 This can be super useful when you want to pick and choose changes made in one branch and bring them over to another branch.

Here's how you can work the cherry-pick magic: 🪄

  1. Branch it out: First, create two new branches and make some commits to each of them. 🌱🌿

  2. Cherry-Pick time: Now comes the fun part! Use git cherry-pick <commit_hash> to cherry-pick the specific commits you want from one branch and magically apply them to the other. 🍒🎆

And just like that, you've selectively added those changes to your target branch without any fuss! 😎🌟

Git cherry-pick is like a "commit picker" – you get to hand-pick commits and bring them together! So handy!

😖Resolving Conflicts:

Conflicts can occur when you merge or rebase branches that have diverged, and you need to manually resolve the conflicts before git can proceed with merge/rebase🔄🌊 . git status command shows the files that have conflicts, git diff command shows the difference between the conflicting versions and git add command is used to add the resolved files🔍🔄.

📝Task-01:

  • Create a new branch and make some changes to it.

  • Use git stash to save the changes without committing them.

  • Switch to a different branch, make some changes and commit them.

  • Use git stash pop to bring the changes back and apply them on top of the new commits.

    📝Task-2:

    • In version01.txt of development branch add below lines after “This is the bug fix in development branch” that you added in Day10 and reverted to this commit.

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

      Commit this with message “ Added feature2.1 in development branch”

    • Line3>> This is the advancement of previous feature

      Commit this with message “ Added feature2.2 in development branch”

    • Line4>> Feature 2 is completed and ready for release

      Commit this with message “ Feature2 completed”

All these commits messages should be reflected in Production branch too which will come out from Master branch (Hint: try rebase).

👨‍💻Solution:

To solve the given task please follow these steps:

  1. Checkout the development branch: git checkout development

  2. Edit version01.txt and add the required lines after "This is the bug fix in the development branch".

  3. Commit the changes with message "Added feature2.1 in the development branch":

     git add version01.txt
     git commit -m "Added feature2.1 in development branch"
    
  4. Add Line3 to version01.txt:

     git add version01.txt
     git commit -m "Added feature2.2 in development branch"
    
  5. Add Line4 to version01.txt:

     git add version01.txt
     git commit -m "Feature2 completed"
    
  6. Checkout the master branch: git checkout master

  7. Create a new production branch from the master branch: git checkout -b production

  8. Cherry-pick the commits from the development branch to the production branch:

     git cherry-pick <commit_hash_of_feature2.1>
     git cherry-pick <commit_hash_of_feature2.2>
     git cherry-pick <commit_hash_of_Feature2_completed>
    
  9. Now the production branch contains the commits with the same messages as in the development branch.

Remember, rebase might not be necessary in this case as we are cherry-picking specific commits from one branch to another. Rebase is typically used to update the base of a feature branch to the latest changes in the main branch.

📝Task-3:

  • In Production branch Cherry pick Commit “Added feature2.2 in development branch” and added below lines in it:

  • Line to be added after Line3>> This is the advancement of previous feature

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

  • Commit: Optimized the feature

👨‍💻Solution:

Sure! Here's the solution to Task-03 in short:

  1. Checkout the Production branch: git checkout production

  2. Cherry-pick the commit "Added feature2.2 in development branch":

     git cherry-pick <commit_hash_of_Added_feature2.2>
    
  3. Edit version01.txt and add the required lines after "This is the advancement of the previous feature":

     Line4>> Added few more changes to make it more optimized.
    
  4. Commit the changes with message "Optimized the feature":

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

Now the Production branch has the commit "Added feature2.2 in development branch" along with the additional changes and the new commit "Optimized the feature."

🎗️Summary:

📚 Today's learning session delved into advanced Git topics, including the powerful "git cherry-pick" and the handy "git stash." 🍒🗃️ Git cherry-pick enables us to selectively pick commits from one branch and apply them to another, streamlining code integration. Meanwhile, git stash lets us temporarily save changes without committing, facilitating smooth context switching between tasks or branches. These advanced Git commands enhance code management and facilitate collaborative workflows.