Mastering Git and GitHub: Advanced Strategies for DevOps Engineers Part 2

Mastering Git and GitHub: Advanced Strategies for DevOps Engineers Part 2

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

Introduction

For DevOps engineers, Git and GitHub are essential tools that let them work together and manage code repositories effectively. We'll explore several advanced Git concepts in this blog article, including git stash, cherry-pick, and conflict resolution, which are very important for DevOps workers. These methods will assist you in maintaining a clean codebase, solving complex problems, and streamlining your workflow.

Git Stash: Managing Temporary Changes

Overview

With the help of the robust feature known as "git stash," you may quickly swap branches or work on a different project by saving your working directory and index. This is particularly useful if you need to swiftly switch between contexts without making rash or unfinished modifications.

Basic Usage

To stash your changes, use the following command:

git stash save "your_message_here"

To apply the stashed changes later, use:

git stash pop

Best Practices

  • Give descriptive messages when stashing changes for clarity.

  • Use git stash list to view a list of stashes and manage them effectively.

Cherry Pick: Selectively Applying Commits

Overview

cherry pick lets you apply particular commits from one branch to another. When you want to add a particular feature or bug fix to a different branch without merging the entire branch, this is helpful.

Basic Usage

git cherry-pick <commit-hash>

Best Practices

  • Always test cherry-picked commits to ensure they integrate seamlessly with the target branch.

  • Remember to commit to the cherry-picked changes after they are applied.

Resolving Conflicts: Navigating Merge Conflicts

Overview

When Git is unable to automatically combine changes from various branches, merge conflicts arise. Understanding how to handle these conflicts is essential for DevOps engineers in order to keep the codebase in good shape.

Basic Steps

  1. Identify Conflicts: Git will mark the conflicting sections in your files. Open the file(s) and locate the conflict markers (<<<<<<<, =======, >>>>>>>).

  2. Resolve Conflicts Manually: Edit the files to remove the conflict markers and choose the desired changes.

  3. Add and Commit: After resolving the conflicts, add the modified files and commit the changes.

Best Practices

  • Use visual tools like GitKraken, SourceTree, or your IDE's built-in merge conflict resolution tools to simplify the process.

  • Communicate with team members to ensure everyone is aware of and on board with conflict resolutions.

Hands-On Practice

Task-01: Branching, Stashing, and Applying Changes

Step 1: Creating a New Branch and Making Changes

# Create a new branch named 'new-feature'
git checkout -b new-feature

# Make necessary changes to your files
# For example, edit or add files in your working directory

Step 2: Using Git Stash for Temporary Storage

# Suppose you need to switch branches without committing changes
# Use git stash to temporarily save your changes
git stash

Now you can safely switch branches.

Step 3: Switching Branches and Making Additional Changes

# Switch to another branch (e.g., 'dev')
git checkout dev

# Make some changes and commit them
# For example, edit or add files in your working directory
git add .
git commit -m "Made changes in dev branch"

Step 4: Bringing Stashed Changes Back

# Return to your original branch ('new-feature')
git checkout new-feature

# Apply the stashed changes on top of your new commits
git stash pop

Now, your original changes are applied on top of the new commits made in the 'dev' branch.

Task-02: Working with Commits and Branches

Step 1: Adding New Features in Development Branch

# Assuming you're in the 'development' branch
# Add new lines to version01.txt
echo "Line2>> After bug fixing, this is the new feature with minor alteration”" >> version01.txt
git add version01.txt
git commit -m "Added feature2.1 in development branch"

echo "Line3>> This is the advancement of previous feature" >> version01.txt
git add version01.txt
git commit -m "Added feature2.2 in development branch"

echo "Line4>> Feature 2 is completed and ready for release" >> version01.txt
git add version01.txt
git commit -m "Feature2 completed"

These commits will be reflected in the 'development' branch.

Step 2: Rebasing in the Production Branch

# Create a new branch from 'master' called 'production'
git checkout -b production

# Use 'git rebase' to bring in the latest changes from 'development'
git rebase development

Now, the 'production' branch will have the latest features from 'development'.

Task-03: Cherry Picking and Modifying Commits

Step 1: Cherry Picking a Commit

# Assuming you're in the 'production' branch
# Cherry pick the commit from 'development'
git cherry-pick <commit-hash-of-"Added feature2.2 in development branch">

Step 2: Adding Lines and Creating a New Commit

  • Open version01.txt and make the required additions.

  • Save the file.

# Add the modified file
git add version01.txt

# Commit the changes with an appropriate message
git commit -m "Optimized the feature"

Now, the 'production' branch has cherry-picked the commit from 'development' and applied additional changes. This optimized feature is now part of the 'production' branch.

Conclusion

Mastering advanced Git techniques is crucial for DevOps engineers to effectively manage code repositories and streamline their workflow. Throughout this blog post, we've explored essential concepts like git stash for managing temporary changes, cherry-pick for selectively applying commits, and conflict resolution for navigating merge conflicts. By understanding these techniques and best practices, DevOps engineers can maintain a clean codebase, efficiently collaborate with team members, and ensure the smooth delivery of projects. With hands-on examples and practical tasks, we've provided a comprehensive guide to leveraging Git and GitHub for success in the DevOps world. Keep practicing and honing your skills to become a proficient Git and GitHub user, empowering your DevOps endeavors.

Happy Learning!