Advance Git & GitHub for DevOps Engineers: Part-2
Welcome to Part-2 of our series on "Advance Git & GitHub for DevOps Engineers." In this installment, we'll delve into more powerful Git commands and techniques, including Git stash, cherry-pick, and resolving conflicts. Let's explore these essential features and how they can enhance your Git workflow.
- Git Stash: Saving Changes Temporarily ๐ฆ
Git stash is a lifesaver when you need to temporarily save changes you've made in your working directory, without committing them. This is particularly useful when you need to switch to a different branch to work on something else but want to preserve your current changes.
How to use Git stash:
Create a new branch and make some changes to it.
Use the command
git stash
to save those changes. This will remove the changes from your working directory and record them in a new stash.You can apply these changes later using
git stash pop
to bring the changes back and apply them on top of the new commits.
Commands for managing stashes:
git stash list
: Shows the list of stashed changes.git stash drop
: Deletes a specific stash.git stash clear
: Deletes all stashes.
- Cherry-pick: Selectively Applying Commits ๐
Git cherry-pick allows you to choose specific commits from one branch and apply them to another. This is valuable when you want to selectively apply changes made in one branch to another.
How to use Git cherry-pick:
Create two new branches and make some commits to each of them.
Use
git cherry-pick <commit_hash>
to select specific commits from one branch and apply them to the other.Resolving Conflicts: Tackling Diverged Branches ๐๐ง
Conflicts can arise when merging or rebasing branches that have diverged, and you need to manually resolve them before proceeding with the merge/rebase.
Resolving Conflicts:
Use
git status
to identify the files that have conflicts.Utilize
git diff
to view the difference between the conflicting versions.Manually edit the files to resolve the conflicts.
Use
git add
to add the resolved files to the staging area.Finally, complete the merge/rebase process with
git commit
.
Task-01: Saving and Applying Changes with Git Stash ๐
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: Feature Integration using Cherry-pick ๐
In
version01.txt
of the development branch, add the following lines after "This is the bug fix in the development branch" that you added in Day10 and then reverted to that commit:Line2>> After bug fixing, this is the new feature with minor alteration
Commit this with the message "Added feature2.1 in the development branch."
Add the following line after "This is the advancement of the previous feature":
Line3>> This is the advancement of the previous feature
Commit this with the message "Added feature2.2 in the development branch."
Add the following line:
Line4>> Feature 2 is completed and ready for release
Commit this with the message "Feature2 completed."
Ensure that all these commit messages are reflected in the Production branch, which is derived from the Master branch (Hint: try rebase).
Task-03: Applying Specific Changes to Production Branch ๐
In the Production branch, cherry-pick the commit "Added feature2.2 in the development branch" and add the following lines to it:
Line to be added after Line3>> This is the advancement of the previous feature
Line4>> Added few more changes to make it more optimized.
Commit this with the message "Optimized the feature."
Conclusion:
In this part of our series, we explored Git stash, cherry-pick, and resolving conflicts, essential tools for DevOps Engineers. By mastering these advanced Git commands, you can efficiently manage changes, integrate features, and tackle conflicts with ease. Stay tuned for Part-3, where we'll delve into even more powerful Git techniques to enhance your development workflow. Happy coding! ๐๐ฉโ๐ป๐จโ๐ป