Table of contents
Git Stash:-
Git stash is a handy command that allows you to temporarily save your local changes without committing them. This is useful when you need to switch to a different branch, pull the latest changes, or work on something else without losing your progress. To use git stash, you simply run:
git stash
This will create a new stash entry and revert your working directory and index to the state of the last commit. You can see the list of stashed entries with:
git stash list
To bring back the changes from the most recent stash entry and apply them on top of the current branch, you can use:
git stash pop
This will also drop the stash entry from the list. Alternatively, you can use:
git stash apply
This will apply the changes but keep the stash entry for later use. You can also specify which stash entry to apply or pop by using its index or name, such as:
git stash pop stash@{1}
To delete a stash entry without applying it, you can use:
git stash drop
You can also clear all the stash entries with:
git stash clear
Git Cherry-pick
Git cherry-pick is a powerful command that allows you to pick a specific commit from one branch and apply it onto another branch. This is useful when you want to integrate only some changes from another branch, without merging or rebasing the whole branch. To use git cherry-pick, you need to specify the commit hash or reference of the commit you want to pick, such as:
git cherry-pick d48e74c # pick commit d48e74c and apply it on top of the current branch
You can also pick multiple commits at once by using a range of references, such as:
git cherry-pick d48e74c..f48e74c # pick all commits from d48e74c (exclusive) to f48e74c (inclusive) and apply them on top of the current branch
By default, git cherry-pick will create a new commit with the same message and author as the original commit. However, you can modify this behavior by using some options, such as:
-e
or--edit
: This will let you edit the commit message before applying it.-x
: This will append a line that says “(cherry picked from commit …)” to the original commit message, indicating where the change came from.-n
or--no-commit
: This will apply the change but not create a new commit, allowing you to make further modifications before committing.-m <parent-number>
or--mainline <parent-number>
: This is used when cherry-picking a merge commit. It specifies which parent of the merge should be considered the mainline, and the change will be replayed relative to that parent.
Sometimes, git cherry-pick may encounter conflicts when applying a commit. In that case, you need to resolve the conflicts manually and then continue the cherry-pick operation with:
git cherry-pick --continue
Alternatively, you can abort the cherry-pick operation and restore the original state with:
git cherry-pick --abort