🌟Introduction :
Welcome back, DevOps Engineers! In this second part of our series on Advance Git & GitHub, we will explore some powerful Git commands and techniques that will help you streamline your development workflow and tackle complex version control scenarios. 😎
Let's dive right in and discover the magic of Git Stash, Cherry Pick, and Resolving Conflicts! 💻
What is Git Stash? 🎒
While working on a project, you might find yourself in a situation where you need to switch branches but have some uncommitted changes in your working directory. Here's where Git Stash comes to the rescue! 🙌
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. You can apply these changes later. git stash list command shows the list of stashed changes.
Basic syntax of Git Stash :
To bring back your changes later, you can use:
What is Cherry Pick? 🍒
Cherry-picking is a useful technique to apply specific commits from one branch to another. This comes in handy when you want to include only specific changes without merging the entire branch.
To use git cherry-pick, you first create two new branches and make some commits to them. Then you use git cherry-pick <commit_hash> command to select the specific commits from one branch and apply them to the other.
To cherry-pick a commit:
How to Resolve Conflicts? ⚔️
Resolving merge conflicts is a common task when working with version control systems like Git. Merge conflicts occur when there are conflicting changes between different branches or commit that Git cannot automatically reconcile. Here's a step-by-step guide on how to resolve merge conflicts:
Pull the Latest Changes: Before attempting to merge, ensure you have the latest changes from the remote repository. Use
git pull
to fetch and merge the changes into your local branch.Open the File(s) with Conflicts: After pulling the changes, Git will notify you if there are any merge conflicts. Open the affected files in your text editor, and you will see conflict markers in the file like this:
Manually Resolve the Conflict: Edit the file to keep the changes you want, or create a new version that incorporates both sets of changes. Remove the conflict markers once you have resolved the conflict.
Add and Commit the Changes: After resolving the conflict, save the file, and then add it to the staging area using
git add
:Complete the Merge: Now that the conflict is resolved and the file is staged, commit the changes:
Push the Merged Changes: If you were merging into a remote branch, push the changes to the remote repository:
Task-01
- 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.ls
Task-02 :
In version01.txt of the 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-03
In the Production branch Cherry pick Commit “Added feature2.2 in development branch” and added the below lines in it:
The line to be added after Line3>> This is the advancement of the previous feature
Line 4>>Added a few more changes to make it more optimized.
Commit: Optimized the feature.
Conclusion :
In conclusion, mastering Advance Git & GitHub commands is a must for any DevOps Engineer seeking to excel in their version control and collaboration skills. In this blog, we explored Git Stash, an essential tool for managing uncommitted changes; Cherry Pick, a powerful technique to selectively apply commits; and Conflict Resolution, a crucial skill for dealing with merging challenges.