Day 11 Task: Advanced Git & GitHub for DevOps Engineers - Part 2
Welcome back to our DevOps journey! In today's installment, we're diving deeper into Git and GitHub, focusing on advanced techniques that every DevOps engineer should master. Let's explore Git stash, cherry-pick, and resolving conflicts.
Git Stash: Saving Work for Later
Have you ever been in a situation where you're working on something in your branch, but suddenly need to switch to another task? Git stash comes to the rescue! It allows you to temporarily save your changes without committing them. Here's how it works:
Create a New Branch: Start by creating a new branch for your work.
Make Changes: Make your desired changes in the branch.
Stash Your Changes: Use the
git stash
command to save your changes. This removes them from your working directory and records them in a stash.Later, Retrieve Your Changes: You can apply these changes later using
git stash pop
.
Remember, you can use git stash list
to see a list of stashed changes and manage them with git stash drop
or git stash clear
.
Cherry-Pick: Selective Commit Application
Git cherry-pick is a powerful command that allows you to choose specific commits from one branch and apply them to another. This is handy when you want to pick and choose particular changes. Here's how it works:
Create Two Branches: Begin by creating two branches.
Make Commits: Make commits in both branches.
Cherry-Pick: Use
git cherry-pick <commit_hash>
to select and apply specific commits from one branch to another.
This allows you to apply only the changes you need from one branch to another, keeping your codebase clean and organized.
Resolving Conflicts: Keeping Your Codebase Smooth
Conflicts can arise when you merge or rebase branches with divergent changes. Resolving conflicts is essential to keep your codebase smooth and error-free. Here's how to tackle conflicts:
Identify Conflicts: Use
git status
to identify files with conflicts.Review Changes: Use
git diff
to see the differences and understand the conflicts.Resolve Conflicts: Manually resolve conflicts in the conflicting files.
Add Resolved Files: Use
git add
to stage the resolved files.Continue the Process: Once conflicts are resolved, continue with the merge or rebase process.
Now, let's tackle the tasks at hand:
Task 01: Stashing and Applying Changes
Create a New Branch: Start by creating a new branch using the
git checkout -b <branch_name>
command. Replace<branch_name>
with your desired branch name.Make Changes: Make the necessary changes to your files in this new branch. This could involve adding, modifying, or deleting files.
Stash Your Changes: Before switching to another branch, stash your changes using the
git stash
command. This will save your changes in a temporary storage area, allowing you to retrieve them later.Switch Branches: Use the
git checkout <other_branch>
command to switch to a different branch where you want to work on something else.Make Changes and Commit: In the new branch, make your desired changes and commit them using
git add <file>
followed bygit commit -m "Your commit message"
.Apply Stashed Changes: Once you're done with the other branch, switch back to the original branch where you stashed your changes using
git checkout <original_branch>
. Then, apply the stashed changes on top of your current changes usinggit stash pop
. This will retrieve the changes you stashed earlier and apply them to your working directory.
Task 02: Reflecting Commits in Production Branch
In this task, you'll ensure that commit messages from one branch are reflected in another using rebase:
Checkout Development Branch: Start by checking out the development branch using
git checkout development
.Edit version01.txt: Open
version01.txt
in your text editor and make the necessary changes as per the task requirements. Remember to add the specified lines and commit each change with the provided commit messages.Rebase onto Master: After committing changes in the development branch, switch to the master branch using
git checkout master
. Then, rebase the development branch onto the master branch usinggit rebase development
. This will apply the commits from the development branch onto the master branch.Resolve Conflicts (if any): If there are any conflicts during the rebase process, resolve them manually as instructed earlier.
Task 03: Cherry-Picking and Optimizing Features
In this task, you'll cherry-pick a specific commit from one branch and add additional changes to it:
Cherry-Pick Commit: Start by checking out the production branch using
git checkout production
. Then, cherry-pick the specified commit usinggit cherry-pick <commit_hash>
.Edit version01.txt: Open
version01.txt
in your text editor and make the necessary additions as instructed. Add the specified lines after the indicated point in the file.Commit Changes: After adding the additional changes, commit them with an appropriate message, such as "Optimized the feature".
Push Changes (if necessary): If you need to push the changes to a remote repository, use
git push
to push the changes to the production branch.
By following these detailed instructions, you'll successfully complete each task and gain a deeper understanding of Git and GitHub for DevOps engineering.
That concludes Part 2 of our series on Git and GitHub for DevOps engineers. Stay tuned for more insights and tips in our ongoing DevOps journey!
Subscribe to my newsletter
Read articles from SWATHI PUNREDDY directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by