Skip to content
Tutorials Center
My learning

Topics

Lesson 28 of 38 ยท Version Control and Tools

Magit: A Complete Git Interface in Emacs

Learn Magit, the Git porcelain for Emacs: open the status buffer, stage hunks and lines, commit, branch, merge, rebase, push and read history with single keys.

Magit is a Git interface that lives inside Emacs, and many people name it as the reason they stay with Emacs. It does not hide Git: every Magit action maps to a real Git command, and you can see the command it ran. What it adds is speed. A single key stages a hunk, another commits, another pushes, and an interactive rebase is an ordinary buffer you edit. This lesson teaches the core workflow.

You need Git installed and the package: M-x package-install RET magit RET. Magit cannot run in the page, so keys and menus are described in prose and code blocks; the runnable blocks are labelled stand-ins that model what Magit manipulates (diff hunks and commit messages). For Git itself, see the Git tutorial on this site.

Opening Magit

Bind one key to the entry point and you are ready:

ELISP
(use-package magit
  :ensure t
  :bind ("C-x g" . magit-status))

C-x g opens the status buffer for the repository of the current file. It lists the current branch, Untracked files, Unstaged changes, Staged changes, and Recent commits. q closes any Magit buffer, and g refreshes it. Press ? at any time to see a transient popup of available commands.

Sections: look without leaving

Each heading is a collapsible section. Move with n/p, and press TAB to fold or expand the section under the cursor, for example opening a file to see its diff. S-TAB cycles all sections, RET on a diff line visits that place in the file. Because everything is plain text in a buffer, the usual Emacs search (C-s), copy and navigation commands work.

Staging and unstaging

Key Action
s Stage the thing at point: a file, a hunk, or the selected region (lines)
u Unstage the thing at point
S / U Stage / unstage everything
k Discard (revert) changes at point, after confirmation

To stage only part of a hunk, select lines with C-SPC and arrows, then s. This is Git's add -p without the prompts. You can leave unrelated edits unstaged and craft clean commits.

JS Stand-in: hunks and partial staging

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Committing

Press c in the status buffer to open the commit popup, then c again to commit. Magit opens a message buffer; write the message and finish with C-c C-c (cancel with C-c C-k). Magit also lets you reuse the message from a previous commit with M-p and M-n. Other commit keys in the popup:

  • a amend the last commit, e amend keeping the message (extend), w reword only the message;
  • f fixup and s squash for use with an autosquash rebase;
  • -a or -S switches add options (stage everything, sign).

The message buffer warns when the first line is too long. The convention is a summary of about 50 characters, a blank line, then a body wrapped at 72.

JS Stand-in: commit message checker

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Branches, merging and rebasing

Press b for the branch popup: b checks out an existing branch (any ref, with completion), c creates and checks out a new one, n creates a branch without checking it out, k deletes. m opens merge, r opens rebase: from there e starts an interactive rebase, where Magit shows the todo list as a buffer and single keys (p pick, r reword, s squash, f fixup, d drop) change each line; C-c C-c finishes. z is the stash popup, t tag, X reset, A cherry-pick.

Remotes: fetch, pull and push

F opens the pull popup and f fetch, P the push popup. The most common sequence after committing is P p (push to the push-remote, usually origin/<branch>), and F u or F p to pull. If no upstream is configured, Magit asks you to choose and can set it for you. Add -f for --force-with-lease in the popup when you rewrote history; Magit shows the exact command in the *magit-process* buffer, which you can open with $.

Reading history

l opens the log popup: l shows the current branch's log, o another branch, a all references. In the log, RET shows the commit, n/p move, and d opens the diff popup for comparing commits. For the history of a single file use M-x magit-log-buffer-file, and for who wrote a line M-x magit-blame (then q to quit). d in the status buffer opens the diff popup, so you can compare any two revisions, branches or a range.

Forge and extras

Pair Magit with Forge to see and create GitHub or GitLab pull requests and issues inside Emacs. magit-todos lists TODO comments from the repository in the status buffer, and git-timemachine steps through a file's past versions. None are required to start.

Learning plan: use C-x g, s, c c, P p for a week. Add b b, l l and F p after that. Press ? whenever you forget a key; the popups document themselves.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which key stages the hunk or file at point in the Magit status buffer?

    s stages, u unstages, c starts a commit, k discards.
  2. 2. How do you finish writing a commit message in Magit?

    C-c C-c finishes the message and creates the commit; C-c C-k cancels it.
  3. 3. You want to stage only two lines of a large hunk. What do you do?

    Selecting a region and pressing s stages just those lines.
  4. 4. Where can you see the exact Git commands Magit ran?

    The process buffer logs each Git command and its output.

Summary

  • C-x g opens the status buffer; ? shows popups; q closes.
  • s/u stage and unstage files, hunks or selected lines; c c commits, C-c C-c finishes.
  • b branches, m merges, r rebases (interactive rebase is a buffer), F/P pull and push.
  • l browses history, M-x magit-blame shows authorship, $ shows the Git commands.
  • Write commit summaries of about 50 characters, bodies wrapped at 72.

Next: Dired and TRAMP: Files Local and Remote