Skip to main content

8.9 Commands for Buffers Visiting Files

Magit defines a few global key bindings unless the user sets magit-define-global-key-bindings to nil. This includes binding C-c M-g to magit-file-dispatch. C-c g would be a much better binding but the C-c <letter> namespace is reserved for users, meaning that packages are not allowed to use it. If you want to use C-c g, then you have to add that binding yourself. Also see Default Bindings and (elisp)Key Binding Conventions.

If you want a better binding, you have to add it yourself:

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

The key bindings shown below assume that you have not improved the binding for magit-file-dispatch.

C-c M-g     (magit-file-dispatch)

This transient prefix command binds the following suffix commands and displays them in a temporary buffer until a suffix is invoked.

When invoked in a buffer that does not visit a file, then it falls back to regular magit-dispatch.

C-c M-g s     (magit-stage-file)

Stage all changes to the file being visited in the current buffer.

C-c M-g u     (magit-unstage-file)

Unstage all changes to the file being visited in the current buffer.

C-c M-g c     (magit-commit)

This transient prefix command binds the following suffix commands along with the appropriate infix arguments and displays them in a temporary buffer until a suffix is invoked. See Initiating a Commit.

C-c M-g D     (magit-diff)

This transient prefix command binds several diff suffix commands and infix arguments and displays them in a temporary buffer until a suffix is invoked. See Diffing.

This is the same command that d is bound to in Magit buffers. If this command is invoked from a file-visiting buffer, then the initial value of the option (--) that limits the diff to certain file(s) is set to the visited file.

C-c M-g d     (magit-diff-buffer-file)

This command shows the diff for the file of blob that the current buffer visits.

user option magit-diff-buffer-file-locked

This option controls whether magit-diff-buffer-file uses a dedicated buffer. See Modes and Buffers.

C-c M-g L     (magit-log)

This transient prefix command binds several log suffix commands and infix arguments and displays them in a temporary buffer until a suffix is invoked. See Logging.

This is the same command that l is bound to in Magit buffers. If this command is invoked from a file-visiting buffer, then the initial value of the option (--) that limits the log to certain file(s) is set to the visited file.

C-c M-g l     (magit-log-buffer-file)

This command shows the log for the file of blob that the current buffer visits. Renames are followed when a prefix argument is used or when --follow is an active log argument. When the region is active, the log is restricted to the selected line range.

C-c M-g t     (magit-log-trace-definition)

This command shows the log for the definition at point.

user option magit-log-buffer-file-locked

This option controls whether magit-log-buffer-file uses a dedicated buffer. See Modes and Buffers.

C-c M-g B     (magit-blame)

This transient prefix command binds all blaming suffix commands along with the appropriate infix arguments and displays them in a temporary buffer until a suffix is invoked.

For more information about this and the following commands also see Blaming.

In addition to the magit-blame sub-transient, the dispatch transient also binds several blaming suffix commands directly. See Blaming for information about those commands and bindings.

C-c M-g e     (magit-edit-line-commit)

This command makes the commit editable that added the current line.

With a prefix argument it makes the commit editable that removes the line, if any. The commit is determined using git blame and made editable using git rebase --interactive if it is reachable from HEAD, or by checking out the commit (or a branch that points at it) otherwise.

C-c M-g p     (magit-blob-previous)

Visit the previous blob which modified the current file.

There are a few additional commands that operate on a single file but are not enabled in the file transient command by default:

command magit-file-rename

This command renames a file read from the user.

command magit-file-delete

This command deletes a file read from the user.

command magit-file-untrack

This command untracks a file read from the user.

command magit-file-checkout

This command updates a file in the working tree and index to the contents from a revision. Both the revision and file are read from the user.

To enable them invoke the transient (C-c M-g), enter "edit mode" (C-x l), set the "transient level" (C-x l again), enter 5, and leave edit mode (C-g). Also see (transient)Enabling and Disabling Suffixes.