Day 11 Task: Advanced Git & GitHub for DevOps Engineers - Part 2

Day 11 Task: Advanced Git & GitHub for DevOps Engineers - Part 2

Mastering Git Stash, Cherry-Pick, and Conflict Resolution Techniques

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

Stash Command in Git - Coding Ninjas

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:

  1. Create a New Branch: Start by creating a new branch for your work.

  2. Make Changes: Make your desired changes in the branch.

  3. 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.

  4. 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:

  1. Create Two Branches: Begin by creating two branches.

  2. Make Commits: Make commits in both branches.

  3. 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:

  1. Identify Conflicts: Use git status to identify files with conflicts.

  2. Review Changes: Use git diff to see the differences and understand the conflicts.

  3. Resolve Conflicts: Manually resolve conflicts in the conflicting files.

  4. Add Resolved Files: Use git add to stage the resolved files.

  5. 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

  1. 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.

  2. Make Changes: Make the necessary changes to your files in this new branch. This could involve adding, modifying, or deleting files.

  3. 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.

  4. Switch Branches: Use the git checkout <other_branch> command to switch to a different branch where you want to work on something else.

  5. Make Changes and Commit: In the new branch, make your desired changes and commit them using git add <file> followed by git commit -m "Your commit message".

  6. 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 using git 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:

  1. Checkout Development Branch: Start by checking out the development branch using git checkout development.

  2. 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.

  3. 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 using git rebase development. This will apply the commits from the development branch onto the master branch.

  4. 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:

  1. Cherry-Pick Commit: Start by checking out the production branch using git checkout production. Then, cherry-pick the specified commit using git cherry-pick <commit_hash>.

  2. 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.

  3. Commit Changes: After adding the additional changes, commit them with an appropriate message, such as "Optimized the feature".

  4. 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!