Introduction
Welcome DevOps enthusiasts. In this blog we're gonna learn more about advanced but less common Git commands. From Git Stash to cherry picking to Resolving Merge Conflicts, this blog got you covered. We'll understand these in the simplest of ways followed by hands on practice by the end to fortify your concepts. So without any further delay, lets learn.
Git Stash
What is git stash?
Git stash is a command that is used to temporarily shelf a changes you made, work on other changes and then come back to it later on. It is used when you want to switch branches or work on a different task without committing the current changes that you made.
Basically Git Stash preserves your changes before commit, without worrying about losing your progress, in case there's a call for another task to be done ASAP.
Use Case
Suppose, you're working on a feature for your project and suddenly your boss orders you to fix a bug on the production line ASAP.
Now, you've made some changes to your code, but they're not quite ready to be committed yet. You don't want to lose your progress, but you also need to switch over to the main branch ASAP to help out.
This is where Git stash comes to your rescue. Using git stash
command you stash away your current changes, like putting them in a little box for safekeeping. And now you're fee to fix the bug without worrying to loose your progress on the feature.
Commands
Stashing or Removing from working directory
git stash
Getting the changes back in the area
git stash pop
List all the stashes
git stash list
To see contents of a stash
git stash show stash@{n}
where,
git stash show: command for showing content
stash@{n}: Specifies which stash you want to inspect. The
{n}
represents the index of the stash, where0
is the most recent stash,1
is the one before that, and so on.
Cherry Pick
What is cherry-pick ?
cherry-pick is the command used for merging a specific commit into the line of development. As the name suggests it points to a specific commit using the hashcode and then get it merged into the main/master branch.
When you're working on different branches in Git, sometimes you come across a commit on one branch that you just have to have on another branch—maybe it's a fix or a feature that's exactly what you need. That's where git cherry-pick
comes in!
It's like picking up the juiciest cherry from a box.
Use Cases:
Debugging in Branches: Imagine you have two branches, development and debugging. You fixed a critical bug in the bug fix branch, and now you need to apply that fix on your development branch without all the other changes. Cherry picking a commit that contains a bug fix allows you to select only that fix together in development with no changes other than the bug fix.
Feature Backporting: Let’s say you were working on a new feature in the feature branch, feature-x, but realize that you need this feature immediately in your stable production branch, master Instead of merging the entire feature-x branch made into a master, which may include unfinished work or other changes that are not ready for release, you can cherry-pick specific commit(s) of the complete feature and apply them to master
Selective Code Review: While in code review, you can see your commits to a feature branch that is particularly well written or solves a problem in the most appropriate way.
Undoing changes: If you want to undo changes made by a particular commit on a branch, you can use git cherry-pick to roll back that commit in reverse order to the same branch this this changes caused by that commit very thoroughly, with no errors or unwanted changes affecting the rest of the branch history Provides a targeted approach to handling.
Managing Release Branches: In a project with multiple release branches you may need to ensure that each release branch has specific bug fixes or updates applied, making them independent Cherry-picking to allow commits to be applied in the option selected on each release branch as needed, ensuring that repairs are extended with no unrelated changes
These are a few Use cases of how git cherry-pick
can be used to selectively apply commits from one branch to another, providing flexibility and control over the development process.
Commands
To cherry-pick a command
git cherry-pick
Resolving Conflicts
What is a conflict?
Conflicts can occur when you merge or rebase branches that have diverged, and you need to manually resolve the conflicts before git can proceed with the merge/rebase.
Git status command shows the files that have conflicts.
Git diff command shows the difference between the conflicting versions.
Git add command is used to add the resolved files.
Hands On Practice
Task Overview
Create a new branch and make some changes to it.
Use git stash to save the changes without committing them.
Switch to a different branch, make some changes and commit them.
Use git stash pop to bring the changes back and apply them on top of the new commits.
Task-01
In version01.txt of development branch add below lines after “This is the bug fix in development branch” that you added in Day10 and reverted to this commit.
Line2>> After bug fixing, this is the new feature with minor alteration”
Commit this with message “ Added feature2.1 in development branch”
Line3>> This is the advancement of previous feature
Commit this with message “ Added feature2.2 in development branch”
Line4>> Feature 2 is completed and ready for release
Commit this with message “ Feature2 completed”
All these commits messages should be reflected in Production branch too which will come out from Master branch (Hint: try rebase).
Read Day 10 for detailed solution.
Task-02
In Production branch Cherry pick Commit “Added feature2.2 in development branch” and added below lines in it:
Line to be added after Line3>> This is the advancement of previous feature
Line4>>Added few more changes to make it more optimized.
Commit: Optimized the feature
Solution
Task - 01:
Committing all the changes individually.
Pushing it to dev branch.
Rebasing to the main branch
We have to push the changes and then it can be seen on the main branch on GitHub.
Task - 02 :
Cherry picking Commit “Added feature2.2 in development branch
Resolved the merge conflict and made changes and pushed the code to GitHub.
GitHub main branch showing changes made after cherry picking
The Working Tree of the repository
Conclusion
So here comes the end of this blog. In summary we covered the major concepts of Git Stash, Cherry-Picking and resolving conflict. Along with that the hands on practice section acted as a cherry on the cake making our concepts even more fun and robust.
Believe you can and you're halfway there. - Theodore Roosevelt
Happy Learning.