Hello Readers,
Here we are Day 11 of #90daysofdevops
👣 Topics for #day11
Git Stash
Git Cherry-Pick
Git Resolving Conflicts
Tasks of Day 11
Git Stash
Git stash is a command that allows you to temporarily save changes that you have made to a branch, without committing those changes.
This can be useful if you need to switch to a different branch to work on something else, but you don't want to lose your progress on the current branch.
Scenario based example on how Git Stash works:
Suppose you are a developer working on a project and you are making changes to a branch. Suddenly, you get an urgent request to work on a different branch to fix a critical bug. You want to switch to the other branch, but you don't want to commit your changes on the current branch yet.
Here's how Git stash can help:
Use Git stash: Run the command
git stash
. This will temporarily save your changes to a stash, so that you can work on a different branch without losing your progress.Switch branches: Run the command
git checkout <other branch>
. This will switch you to the other branch so that you can work on the critical bug.Work on other branch: Make the necessary changes on the other branch to fix the critical bug.
Switch back to original branch: Once you have fixed the bug, switch back to the original branch using the command
git checkout <original branch>
.Use Git stash pop: Run the command
git stash pop
. This will apply the changes that you stashed earlier, so that you can continue working on your progress.Commit changes: Once you have applied the changes, commit them using the command
git commit -m 'completed work on <original branch>'
.
By using Git stash, you can temporarily save changes to a stash and switch to a different branch to work on something else without losing your progress. You can then apply the changes back to the original branch and continue working where you left off.
Git Cherry-Pick
Git cherry-pick is a command that allows you to pick a single commit from one branch and apply it to another branch. It is a useful command for applying specific changes made in one branch to another branch.
Scenario based example on how Git Cherrry-Pick works:
Suppose you are a developer working on a project and you have two branches, feature A and feature B. You have completed work on feature A and have made several commits to that branch. However, there is one commit in feature A that you want to apply to feature B.
Here's how Git cherry-pick can help:
Identify the commit: Use the
git log --oneline
command to identify the commit that you want to cherry-pick. Note the commit hash, which is a unique identifier for that commit.Use Git cherry-pick: Run the command
git cherry-pick <commit hash>
. This will apply the changes made in the commit that you identified in step 1 onto feature B.Resolve conflicts: If there are any conflicts between the two branches, you will need to resolve them before you can continue. Use the
git mergetool
command to resolve any conflicts.Commit changes: Once you have resolved any conflicts, commit the changes using the command
git commit -m 'cherry-picked commit from feature A into feature B'
.
By using Git cherry-pick, you can easily apply specific changes made in one branch to another branch. In this scenario, you identified a single commit that you wanted to apply to feature B and used Git cherry-pick to apply those changes. This can save you time and effort compared to merging the entire feature A branch into feature B, which may include changes that you don't need or want.
Git Conflict Resolution
Git conflict resolution occurs when there are conflicting changes made to the same file in different branches. Git provides tools to help you resolve these conflicts and merge the changes together.
Scenario based example on how Git Conflict Resolution works:
Suppose you are a developer working on a project and you have two branches, feature A and feature B. You have made changes to a file in feature A, and your colleague has made changes to the same file in feature B. When you try to merge feature A into feature B, Git detects a conflict and stops the merge process.
Here's how you can resolve the conflict:
Identify the conflict: Run the command
git status
to identify which file has a conflict. The conflicting file will have a message sayingboth modified
.Use Git mergetool: Run the command
git mergetool
. This will open a visual tool, such as vimdiff or meld, that allows you to see the changes made to the file in both branches side-by-side.Resolve the conflict: In the visual tool, you will need to manually choose which changes to keep and which to discard. You can use the arrow keys to move between the conflicting sections and make changes as needed. Once you have resolved the conflict, save and close the visual tool.
Use Git add: Run the command
git add <filename>
. This will stage the changes that you made to the conflicting file.Commit the changes: Run the command
git commit -m 'resolved conflict in <filename>'
. This will create a new commit that resolves the conflict and allows you to continue merging the branches together.
By using Git conflict resolution tools, you can easily identify and resolve conflicts that occur when merging changes from different branches. In this scenario, you used Git mergetool to visualize the conflicting changes and manually resolve the conflict before committing the changes.
Tasks of Day 11
Solution for Task 1:
Branch is master
Created new Branch called dev
On Master
Switching to dev and making changes to file
Git stash
Solution for Task 2:
Solution for Task 3:
Thank you for reading my Blog. I hope you have learnt something from it! If you find this blog helpful, please like, share, and follow me for more interesting posts like this in the future.
Please navigate to my GitHub Repo: GitHub Repo Link
to check my solutions for the tasks of 90daysofdevops Challenge.