Table of contents
🌱Git Stash
Git stash is a command that allows you to temporarily save changes you have made in your working directory, without committing them. This is useful when you need to switch to a different branch to work on something else, but you don't want to commit the changes you've made in your current branch yet.
Example of how git stash
works:
Suppose you're working on a branch let's say feature in your Git repository and you've made changes to your files but you're not ready to commit them because you want to switch to another branch to fix a bug.
To stash your changes
git stash
This will stash your changes, and gets your working directory clean.
You can switch to another branch now using
git checkout
:git checkout <Next_Branch_name>
After working on the other branch, you then decided to come back to your previous changes. To retrieve your changes from the stash, 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 very flexible when you need to switch 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 2 Branches
git checkout -b devops # Make changes and commits on devops branch git commit -m "Commit 1 on devops" git commit -m "Commit 2 on devops"
git checkout -b feature # Make changes and commits on feature branch git commit -m "Commit 1 on feature" git commit -m "Commit 2 on feature"
Now, apply "Commit 2 of
devops
branch" tofeature
branchfind the commit hash code of "Commit 2 on devops":
git log --oneline #log command show all the commits with hash ID
Switch to feature branch now:
git checkout feature
Cherry-pick the commit from
devops
branch:git cherry-pick <commit_hash_ID>
Resolve conflicts 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
devops
will be cherry-picked ontofeature
.
🌱Conclusion
In summary, git stash
is for temporarily storing changes at backstage, while git cherry-pick
is for selectively applying specific commits to another branch. Both commands are valuable tools in managing your Git workflow efficiently.
Happy Learning😊