Function: magit-unstage

magit-unstage is an interactive and byte-compiled function defined in magit-apply.el.

Signature

(magit-unstage)

Documentation

Remove the change at point from the staging area.

Key Bindings

Source Code

;; Defined in ~/.emacs.d/elpa/magit-20260411.1452/magit-apply.el
;;;; Unstage

(defun magit-unstage ()
  "Remove the change at point from the staging area."
  (interactive)
  (when-let ((s (magit-apply--get-selection))
             (type (magit-diff-type))
             (scope (magit-diff-scope)))
    (pcase (list type scope
                 (magit-apply--ignore-whitespace-p s type scope))
      (`(untracked     ,_  ,_) (user-error "Cannot unstage untracked changes"))
      (`(unstaged    file  ,_) (magit-unstage-intent (list (oref s value))))
      (`(unstaged   files  ,_) (magit-unstage-intent (magit-region-values nil t)))
      (`(unstaged      ,_  ,_) (user-error "Already unstaged"))
      (`(staged    region  ,_) (magit-apply-region s "--reverse" "--cached"))
      (`(staged      hunk  ,_) (magit-apply-hunk   s "--reverse" "--cached"))
      (`(staged     hunks  ,_) (magit-apply-hunks  s "--reverse" "--cached"))
      ('(staged      file   t) (magit-apply-diff   s "--reverse" "--cached"))
      ('(staged     files   t) (magit-apply-diffs  s "--reverse" "--cached"))
      ('(staged      list   t) (magit-apply-diffs  s "--reverse" "--cached"))
      ('(staged      file nil) (magit-unstage-1 (list (oref s value))))
      ('(staged     files nil) (magit-unstage-1 (magit-region-values nil t)))
      ('(staged      list nil) (magit-unstage-all))
      (`(committed     ,_  ,_) (if magit-unstage-committed
                                   (magit-reverse-in-index)
                                 (user-error "Cannot unstage committed changes")))
      (`(undefined     ,_  ,_) (user-error "Cannot unstage this change")))))