Introduction 💫
In this blog, we will learn more about Git. We will cover topics like git stash
, git cherry-pick
and resolve conflicts 🔺. These are some commands that are frequently used in the IT industry by developers. It is important to learn as you have to face these situations very often. 📝
Git Stash 🛍
Think of Git Stash as a secret compartment 🧺🔒 to temporarily hide your changes. It's like tucking your work away, so you can switch tasks without cluttering your current work. When you're in the middle of something and need to switch to another task or branch, use Git Stash. Stashing your work keeps it safe and allows you to return to it later.🔧📦
Assuming you're working on a coding project and you suddenly need to switch to another task without committing your current changes.🔁📊
Check Status: First, use
git status
to see which files you've modified.Stash Your Changes: Use
git stash
to stash away your changes:git stash
Switch Tasks: Now, you can switch to another branch or task without worrying about your ongoing changes.
Check Stash List: To see your stashed changes, use:
git stash list
Reapply Stashed Changes: When you're ready to continue your previous work, use
git stash pop
:git stash pop
Optional: Remove Stashed Changes: If you no longer need the stashed changes after applying, you can remove them with:
git stash delete
Clear All Stashes: To clear all stashed changes, use:
git stash clear
Git Cherry-Picking 🍒🌿
Think of Git Cherry-Picking as handpicking the best fruits 🍒🌿 from another branch and placing them in your basket. It's like bringing specific commits from one branch into another.
Use Git Cherry-Picking when you want to add specific commits from one branch to another. It's handy for grabbing valuable changes without merging entire branches.
How Git Cherry-Picking Works? 🍒🖐️:
Identify the commits you wish to pick from another branch.
Use
git cherry-pick <commit>
to transplant those commits into your current branch.
Hands-On Cherry-Picking! 🚀🍒🔧:
Suppose you have two branches: feature
and main
.
Checkout Target Branch: Switch to the branch where you want to apply the cherry-picked commits.
git checkout main
Cherry-Pick Commits: Use
git cherry-pick <commit_id>
to bring commits from the other branch:git cherry-pick ABC123 git cherry-pick XYZ789
Resolve Conflicts: If there are conflicts, resolve them just like in a merge.
Continue Picking: Continue cherry-picking other desired commits.
Push Changes: Once cherry-picking is done, push the changes to the remote repository.
Resolving Conflicts 🔺
Imagine two people editing the same sentence in a document at the same time. When changes in different branches clash⚔️, Git is unsure which version to choose.
For example⚒:
Let's say you're merging a branch named feature
into main
and conflicts arise.
Identify and Checkout: Identify the conflicted file using
git status
and then usegit checkout <filename>
to open it.Resolve the Conflict: Review and edit the conflicting sections, retaining the desired changes.
Stage and Commit: After resolving, use
git add <filename>
andgit commit
to finalize the resolution.Push Changes: Once resolved and committed, push the changes to the remote repository.
Tasks 📚:
✳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.
Certainly, let's walk through those steps:
Assuming you're working on a Git repository and have two branches: feature-branch
and main
.
Create a New Branch and Make Changes:
git checkout -b feature-branch
Make some changes to your files.
Stash Your Changes:
git stash
Switch to a Different Branch and Make Changes:
git checkout main
Make some changes and commit them.
Apply Stashed Changes:
git stash pop
This will apply the stashed changes on top of your new commits. If there are conflicts, you'll need to resolve them.
Remember, git stash
acts as a temporary storage for your changes. It's like placing your work-in-progress in a safe spot while you handle other tasks.
✳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 commit messages should be reflected in the Production branch too which will come out from the Master branch (Hint: try rebase).
When you made some commits on a feature branch (test branch) and some in the main branch. You can rebase any of these branches. Use the git log command to track the changes (commit history). Checkout to the desired branch you want to rebase then execute the rebase command.
git rebase <branch name>
✳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
Switch to the branch where you want to apply the cherry-picked commits.
Use git cherry-pick <commit_id>
to bring commits from the other branch:
Resolve conflicts and commit with the respective commit message.
✉Endcard:
🎉 Thank you for joining me on this insightful journey into the world of DevOps!
❤ If you found this blog helpful and informative, don't forget to give it a like!
🔄 Share this valuable knowledge with your friends and colleagues, so they can also benefit from understanding the power of DevOps!
👉 Stay updated with my latest posts and never miss out on exciting content! Click that Follow button to join and stay in the loop!
Follow me on Linkedin --> abdallah-qamar 👔
Stay tuned for Day 12...👋