Introduction :
In the world of DevOps, efficient version control is paramount. Git and GitHub are invaluable tools for managing and collaborating on code. However, to fully leverage their power, DevOps engineers should be well-versed in advanced Git concepts such as Git Stash, Cherry-pick, and Resolving Conflicts. In this blog, we will explore these topics and illustrate their practical applications through a series of tasks.
Git Stash:
Imagine you're working on a feature branch, but you need to switch to another branch for an urgent bug fix. You don't want to commit your incomplete work, but you also don't want to lose it. Git Stash comes to the rescue.
Create a new branch and make some changes to it: Start by creating a new branch from your current one and make some code changes.
Use git stash to save the changes without committing them: Execute
git stash
to save your changes in a stash, which acts as a temporary storage.Switch to a different branch, make some changes, and commit them: Switch to the branch where the bug fix is needed, make your changes, and commit them.
Use git stash pop to bring the changes back: Return to your original branch and use
git stash pop
to retrieve your stashed changes and apply them on top of your new commits.
Cherry-pick:
Cherry-picking allows you to select and apply specific commits from one branch to another. This is handy when you need to bring in specific changes without merging an entire branch.
Cherry-picking Commits
Suppose you have a development branch with several commits, and you want to cherry-pick specific ones into your production branch:
Edit version01.txt in the development branch: Add the specified lines and make the commits as instructed.
Ensure commits are reflected in the Production branch: To ensure that the commits made in the development branch are also present in the Production branch, you can use
git rebase
.
Resolving Conflicts:
Conflict resolution is an inevitable part of collaborative development. When two or more branches have made conflicting changes to the same file, Git requires manual intervention to determine the correct version.
Resolving Conflicts
In this task, we'll simulate a conflict resolution scenario:
Cherry-pick a commit: Cherry-pick the commit "Added feature2.2 in development branch" into your production branch.
Edit the cherry-picked commit: Add the specified lines as instructed, which will likely create a conflict in the same area where changes were made in the production branch.
Resolve the conflict: Manually resolve the conflict by editing the file to combine both sets of changes, then commit the resolved version with an appropriate message.
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-02
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).
Task-03
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
Conclusion :
By mastering these advanced Git and GitHub techniques, DevOps engineers can streamline their workflow, collaborate more effectively, and tackle complex version control challenges with confidence. Remember to make your commit messages clear and concise, and feel free to add emojis to make your commit history more expressive. 🚀🔧
With these skills, you'll be better equipped to navigate the intricate world of DevOps and version control, ensuring smooth deployments and efficient collaboration among your team members. Happy coding! 😄👩💻