Day:11 Advance Git & GitHub for DevOps Engineers: Part 2 ๐๐๐
Welcome back to the second part of our series on "Advance Git & GitHub for DevOps Engineers." The previous article explored branching, merging, and rebasing techniques. Today, we'll dive deeper into some more advanced Git features that are crucial for DevOps engineers: Git Stash, Cherry-Pick, and Conflict Resolution. ๐๐ฅ
Git Stash: A Handy Tool for Temporary Changes ๐ ๏ธ
As a DevOps engineer, you often find yourself juggling between different tasks and branches. Git stash comes to your rescue when you need to set aside your current changes without committing them. This is particularly useful when you're in the middle of something but need to switch gears temporarily.
Here's how you can use Git stash:
Create a New Branch and Make Changes: Begin by creating and making changes to a new branch.
Stash Your Changes: When you need to switch to a different branch before committing your changes, use the command
git stash
. This action will stash away your changes and revert your working directory to the last commit.Switch and Commit: Move to the desired branch, make changes, and commit them as needed.
Apply Stashed Changes: To apply your stashed changes, use
git stash pop
. This will reapply the stashed changes on top of your new commits. ๐
Remember, you can list your stashes using git stash list
, delete a stash with git stash drop
, or clear all stashes using git stash clear
. ๐๏ธ
Selective Changes with Git Cherry-Pick ๐
Sometimes, you might need to apply specific commits from one branch to another selectively. Git cherry-pick comes in handy for this precise task.
Here's how you can use Git cherry-pick:
Create Two New Branches: Start by creating two new branches and making commits to them.
Cherry-Pick Commits: If you want to apply certain commits from one branch to another, use
git cherry-pick <commit_hash>
. This will copy the selected commit(s) onto the target branch. ๐
Mastering Conflict Resolution ๐ก๏ธ
Conflicts are inevitable when merging or rebasing branches that have diverged. As a DevOps engineer, you need to be skilled in resolving these conflicts effectively.
Here's a brief guide to conflict resolution:
Check Status: Use
git status
to identify files with conflicts.Analyze Differences: Utilize
git diff
to understand the conflicting changes.Resolve Conflicts: Open the conflicted files, locate the conflicting sections, and manually resolve the differences. Edit the files to keep the desired changes.
Mark Resolved Files: After resolving conflicts, use
git add
to mark the resolved files as ready for the next step. โ
With these advanced Git techniques under your belt, let's move on to some practical tasks to solidify your understanding.
Task-01: Stash and Apply Changes ๐ฆ
Create a new branch and make some changes.
Stash these changes using
git stash
.Switch to a different branch, make changes, and commit them.
Apply the stashed changes using
git stash pop
. ๐
Task-02: Syncing Commit Messages with Rebase ๐
Edit
version01.txt
in the development branch, adding the specified lines and making corresponding commits.Ensure the commit messages are reflected in the Production branch using rebase.
Task-03: Cherry-Pick and Optimize ๐
Cherry-pick the commit "Added feature2.2 in development branch" into the Production branch.
Add the specified lines to the commit message as instructed, emphasizing optimization. ๐