Introduction
Cherry-picking commits in Git allows you to selectively copy individual commits from one branch into another. This is particularly useful when you want to apply specific changes without merging all the changes from another branch. Lazygit simplifies this process with a user-friendly “copy-and-paste” model.
Lazygit
Follow these steps to cherry-pick commits in Lazygit:
-
Select Source Commit:
- Open Lazygit in your repository.
- Navigate to the Branches panel by pressing
2
. - Scroll to the source branch (the one containing the commit(s) you wish to cherry-pick) and press
enter
to switch to the commits panel. - Scroll to the desired commit(s) and press
c
to copy each commit you want to cherry-pick. Copied commits will be highlighted, as shown in the image .
-
Switch to Target Branch:
- Press
esc
to return to the Branches panel. - Ensure the target branch (the one you want to cherry-pick the commits into) is checked out. It should have a
*
next to it. If it’s not checked out, usespace
to select and check it out.
- Press
-
Paste Commits:
- Navigate to the Commits panel by pressing
1
. - Press
v
to paste the cherry-picked commits onto the target branch. The cherry-pick action will be applied, as demonstrated in the image .
- Navigate to the Commits panel by pressing
Basic Terminal
To perform cherry-picking in the terminal:
-
Check out the target branch:
-
Cherry-pick individual commit(s) from the source branch:
Repeat this command for each commit you wish to cherry-pick.
-
Resolve any conflicts that arise during the cherry-pick process, and then finalize the cherry-pick:
Conclusion
Cherry-picking is a powerful feature in Git for selectively applying changes from one branch to another. Lazygit’s intuitive interface makes this process more accessible, especially for beginners. The basic terminal commands offer an alternative method for more experienced users. Remember, cherry-picking should be used judiciously to maintain a clean and understandable history in your repository.