Advance Git & GitHub for Devops Engineers: Part-2


Git Stash:
Git Stash is a command which is used to store the ongoing changes in the working directory without actually committing the file. If a user is working on any file and wants to work on some other branch but also do not want the earlier file to be deleted or committed, he can stash the file i.e. temporarily store the file in the stash. Once his work in other branches is done he can bring back the stashed file back into the working directory and start working on it. Stash changes can be reapplied or dropped as needed. Following are some of the stash commands and their definitions-
Git Commands | Description |
git stash | Stash the file |
git stash list | Shows the list of stashed files |
git stash pop | Bring back the stashed file |
git stash drop | Delete a stash |
git stash clear | Deletes all the stashes |
Cherry-pick:
Cherry-pick is a command that allows to apply any specific commit to any other branch. This is useful when we want to apply specific commits to any different branch without merging the entire branch. Command: git cherry-pick <commit-id>.
Resolving Conflicts:
Conflicts occur when we merge/rebase a branch to one another and the same file contains different contents in the same line. This conflict has to be resolved manually in order to merge/rebase the branch successfully with one another. Following are some of the Git commands useful for resolving conflicts and their definitions-
Commands | Description |
git status | Shows the files that have conflicts |
git diff | Shows the difference between the conflicting versions |
git add | Add the resolved files(for staging) |
Task -1: Stash and Commit:
In this task, our focus will be on branches, and we’ll explore how to utilize the “git stash” command to save changes temporarily without committing them. Simply follow these steps: 🌟
Move to the dev branch using the below command:
In the middle of your work, your team urgently requests you to handle an important task in the main branch. To handle this situation without losing your progress, you can utilize the “git stash” command to save your changes without committing them.
Use following the command to switch to the main branch and proceed with important tasks.
Now that our crucial task is completed, let’s switch 🔙 to the dev branch to continue our work. We’ll use the “git stash pop” command to retrieve the changes we stashed earlier and apply them on top of the new commits.
Task 2: Rebase and Commit Messages
In this task, we’ll be working on the day-10 version01.txt file in the development branch and carrying out a rebase operation. Follow these steps:
📝 Open the
version01.txt
file in the development branch. add the line: "After bug fixing, this is the new feature with minor alteration." Commit this change with the message: “Added feature 2.1 in the development branch.”📝 Add another line in version01.txt: “This is the advancement of the previous feature.” Commit this change with the message: “Added feature2.2 in the development branch.”
📝 Add one more line in version01.txt: “Feature 2 is completed and ready for release.” Commit this change with the message: “Feature2 completed.”
Create a prod branch and use git rebase to include all the commits from the dev branch into the prod branch.
Task 3: Cherry-picking and Resolving conflicts
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:
Switch to prod branch and cherry-pick the commit "Added feature2.2 in development branch" using the below command
Line to be added after Line3>> Added few more changes to make it more optimized. Commit this with message: "Added few more changes to make it more optimized"
🌟Conclusion:
In this blog, we explored three vital Git concepts: Git stash, cherry-pick, and conflict resolution. Git stash allows developers to save work-in-progress without committing, facilitating multitasking. Cherry-pick lets us selectively merge specific commits between branches, making code sharing and bug-fixing efficient. Conflict resolution helps us address conflicts when multiple developers modify the same code, promoting smooth collaboration.
These Git superpowers empower developers to work seamlessly, manage code changes effectively, and achieve project success.🚀💻🌟
Subscribe to my newsletter
Read articles from Ajay Gite directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
