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-v1opens Magit buffers in the current window instead of splitting, except diffs. The default ismagit-display-buffer-traditional.- The
C-x gbinding is redundant: Magit bindsC-x gtomagit-status,C-x M-gtomagit-dispatchandC-c M-gtomagit-file-dispatchitself (optionmagit-define-global-key-bindings, defaultdefault). It only does so when those keys are still free. C-c g→magit-diff-buffer-fileshows the diff for the file I'm editing. Be aware that if you setmagit-define-global-key-bindingstorecommended, Magit wantsC-c gformagit-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:
| Key | Does |
|---|---|
| TAB | Expand or collapse the section or file diff at point |
| s / u | Stage / unstage the file, hunk or region at point |
| S / U | Stage all modified files / unstage everything |
| k | Discard the change at point (asks first) |
| g | Refresh |
| q | Bury the buffer |
| RET | Visit 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
| Keys | Does |
|---|---|
| c c | Commit (opens the message buffer) |
| c a | Amend HEAD, editing the message |
| c e | Extend HEAD: amend without editing the message |
| c w | Reword HEAD's message only |
| c f / c s | Create a fixup / squash commit for a commit you pick |
| c F / c S | The 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
| Keys | Does |
|---|---|
| b b | Check out a branch or revision |
| b l | Check out a local branch, creating it from a remote one if needed |
| b c | Create a new branch and check it out |
| b n | Create a new branch without checking it out |
| b s | Spin off: move unpushed commits to a new branch |
| b m | Rename |
| b k | Delete |
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
| Keys | Does |
|---|---|
| r i | Interactive rebase, starting from a commit you pick |
| r u / r p | Rebase onto the upstream / the push remote |
| r e | Rebase onto another branch ("elsewhere") |
| r s | Rebase a subset of commits |
| r w / r m / r k | Reword / edit / remove one past commit |
| r f | Autosquash 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:
| Key | Does |
|---|---|
| r | Reword |
| e | Edit (stop at this commit) |
| s / f | Squash / fixup into the previous commit |
| d | Drop |
| k | Comment out the line |
| x | Add an exec line |
| M-p / M-n | Move 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
| Keys | Does |
|---|---|
| z z | Stash index and worktree |
| z i / z w | Stash only the index / only the worktree |
| z x | Stash, but keep the index |
| z a / z p | Apply / pop a stash |
| z k | Drop a stash |
| z l | List stashes |
-u in the menu also stashes untracked files.
Bisect: B
- B B starts a bisect: Magit asks for a bad and a good revision and checks out the midpoint.
- Test, then press B B again for bad, or B g for good. B k skips a commit that can't be tested.
- 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 setmagit-auto-revert-modeunder:customand 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.
Leave a comment
Comments are reviewed before they appear.