I've been using magit for a few months now and I like it a lot. But one thing I still do it in a terminal is cherry picking.
What is a simple way to do this?
Emacs Stack Exchange is a question and answer site for those using, extending or developing Emacs. It only takes a minute to sign up.
Sign up to join this communityEverywhere you see a commit in a Magit buffer, you can cherry-pick it by moving point there and then typing A A. You can also cherry-pick multiple commits at once: just select some commits using the region and then press A A.
For both methods, first start up magit-status
.
Method A: Cherry Pick changes from another branch one by one, or by ranges
Press l
and then o
to get a list of other branches.
Select the branch you want to cherry pick from.
Move to the commit you need and press A
followed by A
again. You can also select the lines with the commit range you want with C-space
and press A
followed by A
again.
The status line will show you which commit you selected
e.g. feature/ABC~4
Press Enter
to apply changes.
Method B: Cherry Pick all changes from another branch
A
to choose the cherry pick mode.A
again to apply and commit changes.
Press a
to only apply changes.Enter
.I personally prefer method A as you can handle merge conflicts better.
The workflow was different in earlier versions:
magit-status
b b
) the branch you want to cherry pick into.l r l
) to find the commits that you want to cherry pick. Here you select the 2 branches you want to compare.A
to apply the changes and also stage them togeteher with the commit message. If you press a
it will not stage the changes but only apply them.You don't need to do a log range to cherry pick. Whenever you see a commit log you can press A
to cherry pick it.
l l o
inside the magit buffer instead of l o
, this was causing my git to checkout the other branch instead of just showing its log (however it was hard to tell, because inside the log it looked like i was just looking at the other branch log without checking out). This was messing up my cherry-picking.
I don't use cherry picking, but hitting ?
in magit-status
shows y: Cherry
. This runs the command magit-cherry
, which lets you pick a head and an upstream. It sounds like this is what you want.
You can type C-h r d m Magit RET
to read the Magit manual. You can use C-s cherry
and repeated hit C-s
to search through the manual. Looks like the info is in section 23:
One of the comforts of
git
is that it can tell you which commits have been merged upstream but not locally and vice versa. Git's sub-command for this ischerry
(not to be confused withcherry-pick
). Magit has support for this by invokingmagit-cherry
which is bound toy
by default.Magit will then ask you first for the upstream revision (which defaults to the currently tracked remote branch if any) and the head revision (which defaults to the current branch) to use in the comparison. You will then see a new buffer in which all commits are listed with a directional marker, their revision and the commit message's first line. The directional marker is either
+
indicating a commit that's present in upstream but not in head or-
which indicates a commit present in head but not in upstream.From this list you can use the usual key bindings for cherry-picking individual commits (
a
for cherry-picking without committing andA
for the same plus the automatic commit). The buffer is refreshed automatically after each cherry-pick.
C-h i
instead of C-h r d
. This will take you to the top-level of Info directly.
C-h i
will take you to the last info file you opened, so if I hit C-h i m
, I could potentially be looking at a menu of chapters in SICP instead of a menu of everything. Hitting d
after C-h i
or C-h r
will ensure that you will be in the Info-directory when before you hit m
.
M-x
package-install
RET
sicp
RET
:)
Using Cherry
to better narrow down candidate commits:
Usually you want to pick some commits which are on feature-a
but not on released
, you can:
feature-a-subset
)Cherry
in magit status bufferfeature-a
as cherry head, released
as upstream, to get some log of commits feature-a
differ from released
Apply
->Pick
(A
->A
) on some single commits or a region (see more detail in tarsius and other's answers)