Emacs Magit: Staging, Commits, Rebase, Stash and Bisect, With Every Key Checked

By Lars Nielsen ·

Magit is the Git interface for Emacs: one status buffer where you stage single hunks, commit, rebase, stash and bisect with short key sequences. This guide covers my own config and the keys I use, every one checked against Magit's source, plus the mistakes an earlier version of this article had.

The keys below are from the transient menus and keymaps of the Magit version I have installed (MELPA 20261113.1017, on Emacs 31.1). Press ? or C-h in any Magit menu to see what your version offers. Menus can change between releases.

My configuration

From my Emacs config (config/core/vc.el), in use since April 2025:

(use-package magit
  :ensure t
  :commands (magit-status magit-get-current-branch)
  :defer nil
  :bind (("C-x g" . magit-status))
  :custom
  (magit-display-buffer-function #'magit-display-buffer-same-window-except-diff-v1))

(global-set-key (kbd "C-c g") 'magit-diff-buffer-file)

(use-package diff-hl
  :ensure t
  :after magit
  :config
  (add-hook 'magit-pre-refresh-hook 'diff-hl-magit-pre-refresh)
  (add-hook 'magit-post-refresh-hook 'diff-hl-magit-post-refresh)
  (global-diff-hl-mode 1))
  • magit-display-buffer-same-window-except-diff-v1 opens Magit buffers in the current window instead of splitting, except diffs. The default is magit-display-buffer-traditional.
  • The C-x g binding is redundant: Magit binds C-x g to magit-status, C-x M-g to magit-dispatch and C-c M-g to magit-file-dispatch itself (option magit-define-global-key-bindings, default default). It only does so when those keys are still free.
  • C-c g → magit-diff-buffer-file shows the diff for the file I'm editing. Be aware that if you set magit-define-global-key-bindings to recommended, Magit wants C-c g for magit-dispatch. It won't override a key you've already bound.
  • The two hooks keep diff-hl's change markers in the fringe in step with what Magit stages and commits.

The status buffer

C-x g opens it. It shows the branch, its upstream, and sections for untracked, unstaged and staged changes, plus stashes and unpushed commits. In it:

KeyDoes
TABExpand or collapse the section or file diff at point
s / uStage / unstage the file, hunk or region at point
S / UStage all modified files / unstage everything
kDiscard the change at point (asks first)
gRefresh
qBury the buffer
RETVisit the thing at point (file, commit, stash)

Partial staging is where Magit beats git add -p. Expand a file with TAB, move into a hunk and press s to stage just that hunk. To stage only some lines, mark them with C-SPC and press s.

Committing: c

KeysDoes
c cCommit (opens the message buffer)
c aAmend HEAD, editing the message
c eExtend HEAD: amend without editing the message
c wReword HEAD's message only
c f / c sCreate a fixup / squash commit for a commit you pick
c F / c SThe same, and rebase it into place straight away ("instant")

In the message buffer, C-c C-c commits and C-c C-k cancels. Characters in the summary line beyond git-commit-summary-max-length (default 68) are highlighted, and the diff being committed is shown next to it.

Branches: b

KeysDoes
b bCheck out a branch or revision
b lCheck out a local branch, creating it from a remote one if needed
b cCreate a new branch and check it out
b nCreate a new branch without checking it out
b sSpin off: move unpushed commits to a new branch
b mRename
b kDelete

Branch names are read with your completing-read setup (Vertico, Ivy and so on), so this is a fuzzy search.

Fetch, pull and push: f, F, P

In each menu, u means the upstream and p the push remote, so f u fetches from upstream and P u pushes to it. f a fetches all remotes. In the push menu, -f is --force-with-lease and -F is plain --force: the arguments are visible in the menu before you run anything.

Rebase: r

KeysDoes
r iInteractive rebase, starting from a commit you pick
r u / r pRebase onto the upstream / the push remote
r eRebase onto another branch ("elsewhere")
r sRebase a subset of commits
r w / r m / r kReword / edit / remove one past commit
r fAutosquash the fixup and squash commits

Uncommitted changes are not stashed automatically. Enable -A (--autostash) in the menu for that, or set Git's rebase.autoStash. While a rebase is stopped, the same menu offers r r to continue, r s to skip and r a to abort.

In the todo list that r i opens:

KeyDoes
rReword
eEdit (stop at this commit)
s / fSquash / fixup into the previous commit
dDrop
kComment out the line
xAdd an exec line
M-p / M-nMove the commit up / down

C-c C-c starts the rebase, C-c C-k cancels it.

Log and blame

l l shows the log of the current branch, l b all branches, l o another branch. In a log, RET shows the commit at point, A opens the cherry-pick menu and a applies a commit's changes without committing.

M-x magit-blame annotates the file you're visiting, chunk by chunk. In blame mode, b on a chunk blames the revision before the commit that chunk came from, so you can walk back through a line's history. n / p move between chunks, and q leaves. Plain Git can do the same with git blame <commit>^ -- file, but in Magit it's one key.

Stash: z

KeysDoes
z zStash index and worktree
z i / z wStash only the index / only the worktree
z xStash, but keep the index
z a / z pApply / pop a stash
z kDrop a stash
z lList stashes

-u in the menu also stashes untracked files.

Bisect: B

  1. B B starts a bisect: Magit asks for a bad and a good revision and checks out the midpoint.
  2. Test, then press B B again for bad, or B g for good. B k skips a commit that can't be tested.
  3. Repeat until Git names the first bad commit, then B r to reset.

B s runs a script that decides good or bad for each commit (git bisect run). It does not mark a commit as bad, as an earlier version of this article said.

A few options worth knowing

(use-package magit
  :custom
  ;; Save modified file buffers in the repo without asking (default: t, which asks)
  (magit-save-repository-buffers 'dontask)
  ;; Word-level highlighting in all displayed hunks (default: nil)
  (magit-diff-refine-hunk 'all)
  :config
  ;; Spell-check commit messages
  (add-hook 'git-commit-setup-hook #'git-commit-setup-flyspell))

All three are from Magit's own definitions. The flyspell function is git-commit-setup-flyspell, one of the documented options of git-commit-setup-hook.

Corrections to the earlier version

  • Bisect: bad is B B, not B s or B b. B s runs a script.
  • Branch menu: b l checks out a local branch (it doesn't list branches), delete is b k, and rename is b m.
  • Rebase menu: r e rebases onto another branch, r s rebases a subset, autosquash is r f. There is no automatic stash without -A.
  • Push menu: -f is --force-with-lease.
  • Blame: walking back through history is b, not RET, and plain Git can do it too.
  • Commit messages: the summary limit is 68, not 72. The flyspell hook function is git-commit-setup-flyspell. The old sample set magit-auto-revert-mode under :custom and labelled the summary length as "spell-check", and both are gone.
  • The section on Forge (GitHub and GitLab pull requests) is removed. I don't use Forge and haven't checked its keys.

Related reading

Filed under

Leave a comment

Comments are reviewed before they appear.

Basic HTML allowed: links, emphasis, code and lists. Web addresses become links automatically.