Day 11 #90DaysOf DevOps Challenge
Advanced Git and GitHub:
Git Stash
Git Cherry-pick
Resolving Conflicts
⚡Git Stash:
This command helps you to move your changes from the working directory to temporary storage.
For example, when you are working in a branch-1 and decide to move to another branch-2, but are not ready to stage or commit the changes done in branch-1, then you can use the stash command which will move the changes from the working directory of branch-1 to a temporary location. And you can quickly check out any other branch. Once you decided to resume your work in branch-1, you can unstash the changes and start working again.
Commands:
Git stash
- Save the changes temporarily, and get a clean working directory.
Git stash save <message>
- Save the changes temporarily with a message.
Git stash show <stash id>
- To see the files that are stashed and changes done in them. It helps get the details of any specific stash.
Git stash list
- If we have more than one stash, then It will display all the stashes respectively with different stash IDs.
Git stash apply or git stash apply <stash id>
- Reapply the changes you just applied or any specific stash with its ID.
Git stash pop or git stash pop <stash id>
- It is similar to the stash apply command, the only difference is that it deletes the changes from the stack after re-applying it to the working directory.
Git stash drop or git stash drop <stash id>
- To delete the changes from the stack. Be careful with this command as deleted changes cannot be recovered. By default the latest changes are deleted, if you want to delete any specific stash then use <stash id> with the above command.
Git stash clear
- To delete all the available stashes at once.
Git stash branch <new branch name>
- If you are working on the same branch after stashing then there can be conflicts when re-applying the stash. In this scenario, it is better to stash the changes in a different branch. The above command creates a new branch and stashes the changes in that.
⚡Git Cherry-pick
When we want to add specific commits from one branch to another, then we use the git cherry-pick command.
Syntax:
git cherry-pick <commit id>
This will directly commit the specified revision to the currently checked-out branch. If we want to add more changes, we can use the --no-commit option which will add the changes to the working directory and will not commit it directly.
Use cases:
Separating Branch - If we have multiple features in one branch and we want to create new branches for specific features, then cherry-pick command can be very useful.
If by mistake applied changes to the wrong branch then we can add those changes to the correct branch with the cherry-pick.
If we are facing any issues, and resolution is already present in another branch then that specific commit can be added to the required branch.
Etc.
Disadvantages:
It can create a disjointed history of changes and can make it hard to understand the evolution of the codebase and track down bugs.
Duplicate commits: If we cherry-pick the same commit to different branches then it can create a duplicate codebase.
Cherry-picking individual commits which might be dependent on other changes in the source branch can cause unexpected behaviors and bugs.
It can cause conflicts in the target branch and the resolution can be time-consuming.
Etc.
⚡Conflicts in Git:
Conflict happens when we merge or rebase two diverged branches and they have changes in the same files, affecting the same code. The conflicts are resolved manually in the editor, by choosing what changes to keep and what to drop.
How to resolve the conflicts:
-- Git status command helps in finding, which files have conflicts. Conflicting files will be marked as "both modified" or "both added" in the output.
-- Open the Conflicting File - Open the conflicting file(s) in a text editor. Within the file, you will see sections labeled with "<<<<<<<", "=======", and ">>>>>>>". These sections represent the conflicting changes made by different branches or commit.
-- Modify the File - Edit the conflicting sections manually, removing the conflict markers (<<<<<<<, =======, and >>>>>>>) and keeping the desired changes. Decide how to combine or modify the conflicting changes to resolve the conflict and save the files.
-- Stash the changes - After making changes, you will have to stash the changes with the git add command.
-- Commit the changes.
Task 1 -
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 2 -
- In version01.txt of the development branch add the below lines after “This is the bug fix in the 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 master branch Cherry pick Commit “Added feature2.2 in development branch” and added the below lines in it:
The line to be added after Line 3>> This is the advancement of the previous feature.
Line 4>>Added a few more changes to make it more optimized.
Commit: Optimized the feature.
Summary:
In this article, we learned about Git stash command, cherry-pick command, and how to resolve conflicts. We did some hands-on on these commands for better understanding.
Thank you for reading!📘