git stash Temporarily saves your uncommitted changes (staged and unstaged) and cleans your working directory, so you can switch tasks and come back later. Typical scenario: you are halfway through a feature when an urgent production bug needs fixing on another branch. git stash push -m "WIP: payment validation" # save changes (add -u to include untracked files) git checkout hotfix/login-bug # ...fix, commit, push... git checkout feature/payments git stash pop # reapply and remove from the stash list Useful commands - git stash list: show saved stashes (stash@{0}, stash@{1}...). - git stash apply stash@{1}: reapply a stash but keep it in the list. - git stash pop: apply the latest stash and remove it (if there are conflicts, the stash is kept). - git stash show -p stash@{0}: see the changes in a stash. - git stash drop stash@{0} or git stash clear: delete stashes. - git stash branch new-branch stash@{0}: create a branch from a stash — handy if applying causes conflicts. - git stash push path/to/file: stash only specific files. Caution: stashes are local and easy to forget. For longer interruptions, a work-in-progress commit on your feature branch (later squashed) is often safer. git cherry-pick Applies the changes from a specific existing commit onto your current branch, creating a new commit with the same changes. Typical scenario: a bug fix was committed on the develop branch, and you need the same fix in the release branch without merging all of develop. git checkout release/2.4 git cherry-pick 4f2a9c1 Useful options - Multiple commits: git cherry-pick A B C, or a range: git cherry-pick A^..C. - -x: adds "(cherry picked from commit ...)" to the message for traceability — recommended for backports. - -n (--no-commit): apply changes without committing, to combine several picks. - On conflict: resolve, then git cherry-pick --continue, or --abort to cancel. When to use cherry-pick - Backporting hotfixes to release or maintenance branches. - Recovering a commit made on the wrong branch. - Taking a single useful commit from a branch that is not ready to merge. Cautions - The new commit has a different hash; later merging both branches can produce duplicate changes or confusing conflicts. - Overusing cherry-pick instead of a proper branching strategy makes history hard to follow. Summary - git stash: set aside uncommitted work temporarily. - git cherry-pick: copy specific committed changes to another branch.
Ravindra Wadile · · 0 likes