Function: magit-branch-rename

magit-branch-rename is an autoloaded, interactive and byte-compiled function defined in magit-branch.el.

Signature

(magit-branch-rename OLD NEW &optional FORCE)

Documentation

Rename the branch named OLD to NEW.

With a prefix argument FORCE, rename even if a branch named NEW already exists.

If branch.OLD.pushRemote is set, then unset it. Depending on the value of magit-branch-rename-push-target (which see) maybe set branch.NEW.pushRemote and maybe rename the push-target on the remote.

Key Bindings

Source Code

;; Defined in ~/.emacs.d/elpa/magit-20260822.1158/magit-branch.el
;;;###autoload
(defun magit-branch-rename (old new &optional force)
  "Rename the branch named OLD to NEW.

With a prefix argument FORCE, rename even if a branch named NEW
already exists.

If `branch.OLD.pushRemote' is set, then unset it.  Depending on
the value of `magit-branch-rename-push-target' (which see) maybe
set `branch.NEW.pushRemote' and maybe rename the push-target on
the remote."
  (interactive
    (let ((branch (magit-read-local-branch "Rename branch")))
      (list branch
            (magit-read-string-ns (format "Rename branch '%s' to" branch)
                                  nil 'magit-revision-history)
            current-prefix-arg)))
  (when (string-match "\\`heads/\\(.+\\)" old)
    (setq old (match-str 1 old)))
  (cond ((equal old new)
         (user-error "Old and new branch names are the same"))
        ((and (magit-local-branch-p new) (not force))
         (user-error "Branch `%s' already exists" new)))
  (let* ((push-branch  (magit-get-push-branch old))
         (push-remote  (magit-get "branch" old "pushRemote"))
         (push-default (magit-get "remote.pushDefault"))
         (remote       (or push-remote push-default)))
    (magit-call-git "branch" (if force "-M" "-m") old new)
    (when push-remote
      (magit-set nil "branch" old "pushRemote")
      (magit-set push-remote "branch" new "pushRemote"))
    (when (and (magit-rev-verify (concat remote "/" old))
               (not (magit-rev-verify (concat remote "/" new)))
               (or (eq magit-branch-rename-push-target t)
                   (and-let ((_(eq magit-branch-rename-push-target 'forge-only))
                             (_(require (quote forge) nil t))
                             (_(fboundp 'forge--split-forge-url))
                             (url (magit-git-string "remote" "get-url" remote)))
                     (forge--split-forge-url url)))
               (magit-y-or-n-p (format "Also rename %S to %S on \"%s\"?"
                                       old new remote)))
      (magit-call-git "push" "-v" remote
                      (format "%s:refs/heads/%s" push-branch new)
                      (format ":refs/heads/%s" old))))
  (magit-refresh))