In this article, we will discuss our next part of advanced Git & GitHub for DevOps
So let's begin!!
What is Git Stash?
Let's first discuss what we mean by 'stash', i.e. store something safely in a hidden place. Hence
git stash
is a command temporarily saves your data without commit them. This is useful when you need to switch to a different branch to work on something else, but you don't want to commit the changes you've made in your current branch yet.To use Git stash, you first create a new branch and make some changes to it. Then you can use the command
git stash
to save those changes. This will remove the changes from your working directory and record them in a new stash.Git stash has the following options available:
git stash list git stash apply git stash changes git stash save git stash pop git stash clear git stash branch git stash drop
What is Cherry-pick?
git cherry-pick
is a command that allows you to pick specific commits from one branch and apply it to another. This can be useful when you want to selectively apply changes that were made in one branch to another.To use
git cherry-pick
, you first create two new branches and make some commits to them. Then you use thegit cherry-pick <commit_hash>
command to select the specific commits from one branch and apply them to the other.
What is Resolving Conflicts?
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 andgit add
command is used to add the resolved files.
Task-1
Create a new branch and make some changes to it.
and add the changes in the dev branch but don't commit
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-2
In version01.txt of development branch add the 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 alterations.
Commit this with the message “ Added feature2.1 in development branch”
Line3>> This is the advancement of the previous feature.
Commit this with the message “ Added feature2.2 in development branch”
Line4>> Feature 2 is completed and ready for release.
Commit this with the message “ Feature2 completed”
All these commits messages should be reflected in the Production branch too which will come out from the Master branch (Hint: try rebase).
Task-3
In the Production branch Cherry pick Commit “Added feature2.2 in development branch” and added the below lines in it:
Line to be added after Line3>> This is the advancement of the previous feature
Line4>>Added few more changes to make it more optimized.
Commit: Optimized the feature
Here, I switch to the dev branch and added a new commit "Optimized the feature" which is shown in the git log below. Cherry-picking is nothing but picking one branch's commit to another branch so I switch to the Production branch and cherry-picked here my dev branch's last commit.
Thanks for reading!
Keep Learning and stay healthy.