Git's cherry-pick is for taking particular commits from one branch and replaying them on another. It differs from git merge, which brings over an entire branch's history. With cherry-pick, you choose exactly which changes to move.
A common case is a bug fix. Imagine a development branch (dev) and a main branch (master). A bug is found and fixed on dev, and the same bug may exist on master. You do not want to merge all of dev into master, because dev may contain unfinished work or features that should not appear on master yet. Cherry-pick lets you apply only the fix commit from dev to master, solving the problem quickly and precisely. Another case is porting a feature. If a useful feature was built on a feature branch and is later needed on another branch, cherry-pick the commits that implement it instead of rebuilding it.
Before picking, inspect the commit history with git log. It lists the current branch's commits, including the commit hash, author, date, and commit message. You need the hash of the commit you intend to apply.
For a single commit, use git cherry-pick [commit-hash]. If the hash is abc123def456 and you want to apply that commit to the current branch, run git cherry-pick abc123def456. When there is no conflict, Git applies the change to the current branch and creates a new commit there. The new commit has the same content as the picked commit, but a different hash, and it becomes part of the current branch's history.
Multiple commits can be handled one by one: run git cherry-pick repeatedly with each hash. Or use a range: git cherry-pick [start-commit-hash]..[end-commit-hash]. The start hash is excluded and the end hash is included, so every commit between them is applied to the current branch.
Conflicts can occur when the picked commit changes a file in a way that overlaps with existing changes on the current branch. For example, the picked commit may modify a line that the current branch has also modified differently, so Git cannot merge automatically.
Git will report the conflict. Open each affected file and look for markers such as <<<<<<<, =======, and >>>>>>>. Edit the file manually, remove the markers, and leave the content as desired. This is similar to resolving a merge conflict. After resolving, stage the files with git add [conflict-file-names]. Then run git cherry-pick --continue. If you decide not to proceed, git cherry-pick --abort cancels the operation. Used deliberately, cherry-pick helps move fixes and features across branches without merging unrelated work.