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

Rahul GuptaRahul Gupta
5 min read

Introduction

Efficient code management is crucial in DevOps, and mastering advanced Git commands can significantly enhance your workflow. This blog explores three essential Git techniques: git stash, git cherry-pick, and conflict resolution. These commands help you temporarily save work, selectively apply changes across branches, and effectively resolve merge conflicts. By integrating these tools, you can improve productivity, collaboration, and code quality. Join us as we provide practical examples and step-by-step guides to leverage these advanced Git commands, ensuring a streamlined and efficient development process.

Git Stash

Git stash is an essential tool for developers to temporarily save changes in their working directory without committing them. This is particularly useful when you need to switch to another branch to work on something else but don't want to lose your current progress. Here’s a step-by-step guide to using git stash:

  1. Create a new branch and make changes:

     git checkout -b new-feature-branch
     # Make some changes in your files
     echo "Some changes" >> file.txt
    
  2. Usegit stash to save changes:

     git stash
     # The changes are now saved in the stash and removed from the working directory
    
  3. Switch to a different branch, make and commit changes:

     git checkout main
     echo "Changes in main branch" >> file.txt
     git add file.txt
     git commit -m "Made changes in main branch"
    
  4. Usegit stash pop to bring the stashed changes back:

     git stash pop
     # The stashed changes are reapplied to your working directory
    

Cherry-pick

Cherry-pick allows you to apply specific commits from one branch to another. Here's how you can use it:

  1. Create branches and make commits:

     git checkout -b branch1
     echo "Commit in branch1" >> file.txt
     git add file.txt
     git commit -m "Commit in branch1"
    
     git checkout -b branch2 main
     echo "Commit in branch2" >> file2.txt
     git add file2.txt
     git commit -m "Commit in branch2"
    
  2. Cherry-pick a commit from branch1 to branch2:

     git checkout branch2
     git cherry-pick <commit_hash_from_branch1>
    

Resolving Conflicts

Conflicts occur when merging or rebasing branches. Here’s how to resolve them:

  1. Identify conflicts:

     git status
     # This will show files with conflicts
    
  2. View differences:

     git diff
     # This shows the conflicting changes
    
  3. Resolve conflicts manually, then add resolved files:

     # Edit the conflicting files to resolve conflicts
     git add resolved_file.txt
    

Task-01: Using Git Stash

  1. Create a new branch and make changes:

     git checkout -b feature-branch
     echo "Some new feature" >> feature.txt
    
  2. Stash the changes:

     git stash
    
  3. Switch to a different branch, make changes, and commit:

     git checkout main
     echo "Changes in main" >> main.txt
     git add main.txt
     git commit -m "Changes in main"
    
  4. Apply stashed changes:

     git stash pop
    

Task-02: Adding Commits and Rebasing

  1. Add new lines inversion01.txt on the development branch:

     git checkout development
     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"
    
  2. Rebase development branch commits onto the production branch:

     git checkout production
     git rebase development
    

Task-03: Cherry-pick and Optimization

  1. Cherry-pick the specific commit:

     git checkout production
     git cherry-pick <commit_hash_for_"Added feature2.2 in development branch">
    
  2. Add additional optimization lines:

     echo "Line4>>Added few more changes to make it more optimized." >> version01.txt
     git add version01.txt
     git commit -m "Optimized the feature"
    

Optimizing Features with Cherry-pick in Git

In the world of DevOps and continuous integration, maintaining clean and efficient code is paramount. One of the powerful tools in Git that helps achieve this is the cherry-pick command. In this blog post, we'll explore how to selectively apply changes across branches and optimize features seamlessly.

Why Use Cherry-pick?

Cherry-picking allows you to apply specific commits from one branch to another. This is particularly useful when you want to incorporate only certain changes from a branch without merging all the changes.

Scenario: Optimizing a Feature

Imagine you're working on a development branch where you've added several new features. Some of these features are ready for production, but others need further refinement. Here's how you can cherry-pick specific commits and optimize them.

Step-by-Step Process

  1. Identify the Commit: First, identify the commit you want to cherry-pick. For instance, let's say we have a commit with the message "Added feature2.2 in development branch."

  2. Switch to the Target Branch:

     git checkout production
    
  3. Cherry-pick the Commit:

     git cherry-pick <commit_hash>
    

    This applies the specific changes from the identified commit to the production branch.

  4. Optimize the Feature: After cherry-picking, you may want to make additional optimizations. For example, add new lines to enhance the feature's performance:

     echo "Line4>>Added few more changes to make it more optimized." >> version01.txt
     git add version01.txt
     git commit -m "Optimized the feature"
    

Benefits of Cherry-picking and Optimization

  • Selective Integration: Only incorporate necessary changes without merging entire branches.

  • Focused Enhancements: Improve specific features without disrupting the workflow.

  • Efficient Workflow: Maintain a clean history and streamlined development process.

Conclusion

Mastering advanced Git techniques is crucial for efficient DevOps workflows. This post covered essential commands like git stash, git cherry-pick, and conflict resolution.

Git Stash: Temporarily save your work without committing, allowing seamless context switching and progress retention.

Cherry-pick: Selectively apply changes from one branch to another, ensuring only necessary updates are integrated, maintaining a clean codebase.

Conflict Resolution: Use git status, git diff, and git add to manage and resolve conflicts, ensuring consistency and reliability.

These techniques empower you to maintain control over your development process, improve collaboration, and keep your production environment optimized. Mastering these commands will enhance your productivity and ensure project success. Stay tuned for more advanced Git and GitHub insights for DevOps!

Thank you for reading our DevOps blog post. We hope you found it informative and helpful. If you have any questions or feedback, please don't hesitate to contact us.

I hope this helps!

Happy Learning

0
Subscribe to my newsletter

Read articles from Rahul Gupta directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Rahul Gupta
Rahul Gupta

Hey there! 👋 I'm Rahul Gupta, a DevOps Engineer passionate about all things AWS DevOps Technology. Currently, on a learning adventure, I'm here to share my journey and Blogs in the world of cloud and DevOps. 🛠️ My focus? Making sense of AWS services, improving CI/CD, and diving into infrastructure as code. Whether you're fellow interns or curious enthusiasts, let's grow together in the vibrant DevOps space. 🌐 Connect with me for friendly chats, shared experiences, and learning moments. Here's to embracing the learning curve and thriving in the exciting world of AWS DevOps Technology!