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

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

Introduction

Ever felt overwhelmed by juggling multiple tasks in your DevOps pipeline? Uncommitted changes piling up, conflicting branches causing headaches, and cherry-picking the perfect features adding complexity to your workflow? Fear not, fellow developer! This guide equips you with the essential Git commands to conquer these challenges and streamline your DevOps experience. Dive into the world of Git Stash, Cherry-pick, and Conflict Resolution - your secret weapons for managing changes with finesse. Prepare to unlock a new level of efficiency and collaboration as you master these powerful techniques!

Git Stash: A Temporary Refuge for Changes

Concept:

Git Stash provides a way to temporarily shelve uncommitted changes in your working directory. It's like putting your unfinished work aside but keeping it readily accessible for later use.

Command:

git stash

Examples:

  • You're working on a feature branch but need to switch to a bug fix in another branch. Stash your incomplete feature changes before switching, and you can resume working on them later.

  • You're collaborating on a project and want to share your current progress without committing unfinished work. Stash your changes to share a clean working directory.

Git Cherry-pick: Selecting Specific Changes

Concept:

Git Cherry-pick allows you to selectively apply individual commits from one branch into another. This is useful when you want to incorporate specific changes from a branch without merging the entire branch history.

Command:

git cherry-pick <commit_hash>

Examples:

  • You have a development branch with several bug fixes. You want to apply just the latest bug fix to the production branch without merging other changes. Use cherry-pick to select and apply the individual commit.

  • You're working on a long-lived feature branch and want to incorporate a specific fix from another branch related to the feature. Use cherry-pick to integrate the fix without merging the entire feature branch history.

Resolving Conflicts: Merging Divided Paths

Concept:

Conflicts arise when merging or rebasing branches that have diverged significantly. These conflicts occur when the same lines of code have been modified differently in separate branches. Git highlights conflicting sections in files, and you'll need to manually resolve these discrepancies before proceeding.

Commands:

  • git status: Shows the files with conflicts.

  • git diff: Displays the differences between conflicting versions of a file.

  • git add: Adds a resolved file back to the staging area.

Examples:

  • You're merging a feature branch into the master branch, but both branches modified the same function. Git will identify the conflicting lines, and you'll need to manually edit the code to combine the best aspects of both versions.

  • You're rebasing a development branch onto the master branch, but both branches made changes to the same configuration file. Resolving these conflicts ensures a clean integration of changes.

Tasks: Putting Your Skills to Practice

Task-01: Mastering Git Stash

Solution:

  1. Create a new branch (git checkout -b feature1) and make changes to a file (echo "Changes for feature 1" >> file1.txt).

  2. Stash the changes (git stash).

  3. Switch to a different branch (git checkout main), make changes to another file (echo "Changes for main branch" >> file2.txt), and commit them (git commit -m "Changes made in main branch").

  4. Retrieve and apply the stashed changes (git stash pop). This integrates the feature branch changes onto the current state of the main branch.

Task-02: Adding Features and Rebasing

Solution:

  1. Switch to the development branch (git checkout development).

  2. Add new features with appropriate commit messages:

    • echo "After bug fixing, this is the new feature with minor alteration" >> version01.txt (Commit: "Added feature2.1 in development branch")

    • echo "This is the advancement of previous feature" >> version01.txt (Commit: "Added feature2.2 in development branch")

    • echo "Feature 2 is completed and ready for release" >> version01.txt (Commit: "Feature2 completed")

  3. Rebase the development branch onto the updated main branch:

    • Update the local main branch (git checkout main; git pull origin main).

    • Switch back to development and rebase (git checkout development; git rebase main). Resolve any conflicts that arise during the rebase.

This approach ensures the development branch incorporates the latest changes from master while maintaining a linear commit history.

Task-03: Cherry-picking with Optimization

Solution:

  1. Switch to the production branch (git checkout production).

  2. Identify the specific commit hash for "Added feature2.2 in development branch" and cherry-pick it (git cherry-pick <commit_hash>).

  3. Add additional lines and commit the optimized version:

    • echo "Line to be added after Line3 >> This is the advancement of previous feature" >> version01.txt

    • echo "Line4 >> Added few more changes to make it more optimized." >> version01.txt

    • git commit -m "Optimized the feature"

Cherry-picking allows you to selectively integrate specific features from another branch while maintaining independence in the production branch.

Conclusion

By mastering Git Stash, Cherry-pick, and Resolving Conflicts, you gain valuable tools for managing changes effectively in your DevOps environment. These techniques enable you to maintain a clean and organized codebase while facilitating collaboration and seamless integration throughout the development lifecycle.