Table of contents
1. ๐ Greetings, Adventurers! ๐
Welcome back to Day 11 of our exhilarating #90DaysOfDevOps adventure! Todayโs expedition takes us deep into the realm of Git mastery, where weโll uncover the hidden treasures of Git Stash and Cherry-pick techniques. Moreover, we shall unravel the art of resolving merge conflicts, an invaluable skill for seamless collaboration in your quests. Prepare yourselves to grasp the secrets of Stashing and Applying Changes, attaining virtuosity in the Rebase command, and honing the craft of effective commit messages. As we traverse further, behold the wondrous magic of Cherry-picking and unleashing the full potential of features in your noble projects. Together, letโs embark on this thrilling journey of version control, elevating your DevOps experience to unparalleled heights! ๐๐โ
2. ๐ What is Git Stash?
Imagine you are working on a coding project, and suddenly your team asks you to fix a critical bug in another part of the code. You donโt want to commit your incomplete changes, as it might create issues in the main codebase. ๐
Here comes Git Stash to the rescue! ๐
Git Stash is a powerful feature that allows you to temporarily store your unfinished changes in a safe, hidden box. When you use the โgit stashโ command, Git saves your current changes and reverts your workspace to a clean state, as if you never started making those changes. ๐ฆ
This means you can switch to the bug fix task or any other task without worrying about your incomplete changes interfering with your work. The changes you stashed away are stored and can be easily retrieved later. Once youโve fixed the bug or completed the other task, you can simply unstash your changes and continue where you left off, with all your work intact! ๐งฉ
Itโs like having a secret compartment for your code changes, allowing you to work on multiple tasks simultaneously and keeping your codebase organized and free from half-baked changes. ๐งน
3. ๐ What is Cherry-pick?
In Git, โcherry-pickโ is a useful operation that allows you to select and apply specific commits from one branch to another.
Imagine you have two branches in your project, and thereโs a bug fix or a cool feature in one branch that you want to add to another branch without merging the whole branch. ๐
Hereโs where cherry-pick comes to the rescue! ๐
You can cherry-pick the specific commit from one branch and apply it to another branch. Itโs like plucking a cherry from one tree and placing it on another tree! ๐๐ณ
Cherry-pick is a nifty tool to selectively bring in changes from one branch to another, making it easier to manage code and collaborate with your team. ๐ฉโ๐ป๐จโ๐ป
4. ๐ How to resolve merge Conflicts?
Merge conflicts can happen when you try to combine different branches, and Git doesnโt know which changes to keep. This can occur when youโre merging or rebasing branches that have gone in different directions.
When a conflict occurs, Git will let you know which files have conflicts. You can use the โgit statusโ command to see the list of conflicted files. ๐ฎ
Next, you can use the โgit diffโ command to see the differences between the conflicting versions and understand what needs to be resolved. ๐
Now comes the fun part! You get to manually edit the conflicting parts in the files, choosing which changes to keep and which to discard. Once youโve made the necessary adjustments, you use the โgit addโ command to tell Git that youโve resolved the conflicts. ๐ ๏ธ
After resolving all the conflicts, you can continue with the merge or rebase process with confidence! ๐
By resolving merge conflicts, you can ensure that your code is in sync and ready to be shared with the rest of the team. ๐ฉโ๐ป๐จโ๐ป
5. ๐ Task 1: Stashing and Applying Changes
In this task, our focus will be on branches, and weโll explore how to utilize the โgit stashโ command to save changes temporarily without committing them. Simply follow these steps: ๐
- Create a new branch ๐ฟ using the below command:
git checkout -b dev
2. Make some changes to the files ๐ in the dev branch.
3. In the middle of your work, your team urgently requests you to handle an important task in the main branch. To handle this situation without losing your progress, you can utilize the โgit stashโ command to save your changes without committing them.
4. Use following the command to switch to the main branch and proceed with important tasks.
5. Now that our crucial task is completed, letโs switch ๐ to the dev branch to continue our work. Weโll use the โgit stash popโ command to retrieve the changes we stashed earlier and apply them on top of the new commits.
6. ๐ Task 2: Rebase and Commit Messages
In this task, weโll be working on the day-10 version01.txt file in the development branch and carrying out a rebase operation. Follow these steps:
- ๐ Open the
version01.txt
file in the development branch. add the line: "After bug fixing, this is the new feature with minor alteration." Commit this change with the message: โAdded feature 2.1 in the development branch.โ
2. ๐ Add another line in version01.txt: โThis is the advancement of the previous feature.โ Commit this change with the message: โAdded feature2.2 in the development branch.โ
3. ๐ Add one more line in version01.txt: โFeature 2 is completed and ready for release.โ Commit this change with the message: โFeature2 completed.โ
4. ๐ Create a prod branch and use git rebase to include all the commits from the dev branch into the prod branch.
7. ๐ Task 3: Cherry-picking and Optimizing Features
In this task, weโll learn about cherry-picking a specific commit from one branch to another and making additional changes to it. Follow these steps:
- ๐ Switch to the production branch. And cherry-pick the commit โAdded feature2.2 in development branchโ using the below command.
git cherry-pick <commit-hash>
2. โ๏ธ Add the following lines after โThis is the advancement of the previous featureโ in the version01.txt file:
Added few more changes to make it more optimized.
3. ๐ Commit this change with the message โOptimized the feature.โ
8. ๐ Conclusion ๐
Congratulations on completing Day 11 of the #90DaysOfDevOps challenge! During this exciting session, we explored some advanced Git techniques, such as stashing, cherry-picking, and rebasing, and weโre going to celebrate your accomplishments!
Git Stash proved to be an invaluable tool, allowing us to temporarily save changes without committing them. Cherry-Pick, on the other hand, empowered us to transfer selective commits between branches, making it a breeze to manage our codebase effectively. And letโs not forget about mastering conflict resolution, a crucial skill that enhances collaboration and teamwork.
The comprehensive step-by-step guide covered everything you need to know about stashing and applying changes, as well as how to use rebase to maintain a clean and organized commit history. With the power of cherry-picking, we were able to optimize and fine-tune features, which undoubtedly will boost our development productivity.
By incorporating these powerful Git tools into our workflows, we can streamline processes and make collaboration among team members seamless and efficient.
As you continue on your DevOps journey, I encourage you to keep exploring and practicing with Git, as it holds immense potential for DevOps engineers like yourself. Stay excited and motivated, for there are more challenges ahead that will only help you grow stronger as a developer and a team player! ๐ช๐๐