Table of contents
🔸Git Stash
In simple terms, git stash
is like putting your changes aside temporarily. It allows you to save your work without committing it, so you can switch to another task or branch and then come back to your changes later. It's useful when you're not ready to commit your changes but need to work on something else or clean up your workspace.
Here's a simple example of how git stash
works:
Let's say you're working on a feature branch in your Git repository and you've made some changes to your files but you're not ready to commit them yet because you need to switch to another branch to fix a bug:
Check the status of your changes:
git status
Stage your changes (if necessary) using
git add
.Now, to stash your changes, run:
git stash
This will stash your changes, leaving your working directory clean.
You can switch to another branch now using
git checkout
:git checkout other-branch
After working on the other branch, you might decide to come back to your previous changes. To retrieve your changes from the stash, you can use:
git stash apply
This will reapply your changes to the current branch.
If you want to remove the changes from the stash after applying them, you can run:
git stash drop
Or, if you want to apply and drop in a single command, you can use:
git stash pop
This is a basic example of how git stash
can be used to temporarily store changes in Git. It's particularly handy when you need to switch contexts or work on something else briefly without committing half-finished work.
🔸Git Cherry-pick
Git cherry-pick is a command that allows you to select specific commits from one branch and apply them to another. This can be useful when you want to selectively apply changes that were made in one branch to another.
Example:
Create two new branches:
git checkout -b branch1 # Make some changes and commits on branch1 git commit -am "Commit 1 on branch1" git commit -am "Commit 2 on branch1"
git checkout -b branch2 # Make some changes and commits on branch2 git commit -am "Commit 1 on branch2" git commit -am "Commit 2 on branch2"
Now, suppose you want to apply "Commit 2 on branch1" to
branch2
:First, find the commit hash of "Commit 2 on branch1":
git log
Identify the hash of "Commit 2 on branch1".
Switch to
branch2
:git checkout branch2
Cherry-pick the commit from
branch1
:git cherry-pick <commit_hash>
Replace
<commit_hash>
with the actual hash of "Commit 2 on branch1".Resolve conflicts if any. Git might pause the cherry-pick process if there are conflicts to resolve. You'll need to manually resolve these conflicts, stage the changes, and continue the cherry-pick with
git cherry-pick --continue
.Once conflicts are resolved (if any), the commit from
branch1
has been cherry-picked ontobranch2
.🔸Conclusion
In summary,
git stash
is for temporarily shelving changes, whilegit cherry-pick
is for selectively applying specific commits to another branch. Both commands are valuable tools in managing your Git workflow efficiently.
I believe this blog will be really helpful, giving you fresh perspectives and teaching you something new and interesting. 🙏
😊 Enjoy learning!