Advanced Git & GitHub for DevOps Engineers: Part-2 Git stash, Cherry pick, Resolving conflicts

Introduction: As a DevOps engineer, having a solid understanding of advanced Git and GitHub techniques is crucial to streamline your development workflow. In this blog, we'll delve into three essential topics: Git stash, cherry-pick, and resolving conflicts. Let's explore each concept with clear and concise examples to help you grasp these powerful tools effectively.

  1. Git Stash:

Git stash is a lifesaver when you need to switch branches while working on an unfinished feature or bug fix. It allows you to temporarily store your changes without committing them.

Command:

# Stash changes
git stash

# Apply stashed changes
git stash apply

# List stashed changes
git stash list

Example: Imagine you're working on a new feature branch, but your team reports a critical bug on the main branch that needs immediate attention. Instead of committing your incomplete changes, use Git Stash to store them temporarily. Once the bug is fixed on the main branch, you can switch back to your feature branch and apply the stashed changes to continue your work seamlessly.

  1. Cherry-Pick:

Cherry-pick enables you to pick specific commits from one branch and apply them to another. This is helpful when you want to bring specific changes into a different branch without merging the entire branch.

Command:

# Cherry-pick a commit
git cherry-pick <commit-hash>

Example: Suppose you have a development branch where new features are implemented. Your team just released version 1.0 from the 'release' branch. Now, you want to add a critical bug fix from the 'release' branch into the 'development' branch. You can cherry-pick the commit containing the bug fix onto the 'development' branch, incorporating only that specific change into your codebase.

  1. Resolving Conflicts:

Conflicts may arise when merging or rebasing branches with divergent changes. Resolving conflicts involves making decisions about how to combine conflicting changes and ensuring the final code is correct.

Steps to Resolve Conflicts:

  1. Identify conflicting files using git status.

  2. Open the conflicting file(s) and look for markers like <<<<<<<, =======, and >>>>>>>.

  3. Modify the conflicting lines to resolve conflicts manually.

  4. Add the resolved files using git add.

  5. Complete the merge or rebase with git commit.

Example: You and your teammate are working on separate branches, and both of you modified the same function in different ways. When you try to merge your changes, Git detects a conflict. Open the conflicting file, review the changes made by both of you and decide how the final function should look. Remove the conflict markers and save the file. After resolving all conflicts, commit the changes, and the merge will be successful.

Let's look at a few examples for all of the above:

  • Create a new branch and make some changes to it:
sudo git branch Dev
sudo git checkout Dev


Use git stash to save the changes without committing them

git command helps to save our latest changes in temporary storage without committing them and makes our branch clean again.

sudo git stash


Switch to a different branch, make some changes and commit them

  • To create and switch to new branch,
sudo git branch main
sudo git checkout main


Use git stash pop to bring the changes back and apply them on top of the new commits

sudo git stash pop
cat file.txt

By executing the above command, our changes back from stash.

Now, Add and commit the file,

sudo git add file.txt
sudo git commit -m "main branch commit"


In version01.txt of the development branch add the below lines after

  • Line2>> After bug fixing, this is the new feature with minor alterations”

    Commit this with the message “ Added feature2.1 in development branch”

vi Version01.txt

Add the below content,

After bug fixing, this is the new feature with minor alteration
  • Add and commit changes,

COPY

sudo git add Version01.txt
sudo git commit -m "Added feature2.1 in development branch"
  • Line3>> This is the advancement of previous feature

    Commit this with message “ Added feature2.2 in development branch”

vi Version01.txt

Add the below content,

This

PY is the advancement of previous feature

  • Add and commit changes,
sudo git add Version01.txt
sudo git commit -m "Added feature2.2 in development branch"
  • Line4>> Feature 2 is completed and ready for release

    Commit this with message “ Feature2 completed”

vi Version01.txt

Add the below content,

Feature 2 is completed and ready for release
  • Add and commit changes,
sudo git add Version01.txt
sudo git commit -m "Feature2 completed"

All these commits messages should be reflected in the Production branch too which will come out from the Master branch

Now, all commits should reflect in the production branch.

  • Switch to the Production branch
sudo git checkout Production
sudo git rebase Develop

Please refer below screenshot for reference,


In the Production branch, Cherry-pick commit “Added feature2.2 in development branch”

  • To cherry-pick from in production branch, first do
sudo git log

and copy HashID which you want to cherry-pick.

sudo git cherry-pick <hashID>

  • add the below lines in it:

  • line to be added after Line3>> This is the advancement of previous feature

  • Line4>>Added a few more changes to make it more optimized.

  • Commit: Optimized the feature

vi Version01.txt

Add the below lines,

Added few more changes to make it more optimized.

Add and Commit file,

sudo git add Version01.txt
sudo git commit -m "Optimized the feature"

Conclusion: Mastering Git stash, cherry-pick, and conflict resolution will empower you as a DevOps engineer to manage complex development scenarios efficiently. These powerful Git features allow you to handle work seamlessly, pick the right changes for different branches, and ensure a smooth collaboration process within your team. With these techniques in your arsenal, you'll be better equipped to tackle challenging version control situations and contribute effectively to your projects.