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-20260411.1452/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)))
  (when (equal old new)
    (user-error "Old and new branch names are the same"))
  (magit-call-git "branch" (if force "-M" "-m") old new)
  (when magit-branch-rename-push-target
    (let ((remote (magit-get-push-remote old))
          (old-specified (magit-get "branch" old "pushRemote"))
          (new-specified (magit-get "branch" new "pushRemote")))
      (when (and old-specified (or force (not new-specified)))
        ;; Keep the target setting branch specified, even if that is
        ;; redundant.  But if a branch by the same name existed before
        ;; and the rename isn't forced, then do not change a leftover
        ;; setting.  Such a leftover setting may or may not conform to
        ;; what we expect here...
        (magit-set old-specified "branch" new "pushRemote"))
      (when (and (equal (magit-get-push-remote new) remote)
                 ;; ...and if it does not, then we must abort.
                 (not (eq magit-branch-rename-push-target 'local-only))
                 (or (not (eq magit-branch-rename-push-target 'forge-only))
                     (and (require (quote forge) nil t)
                          (fboundp 'forge--split-forge-url)
                          (and$ (magit-git-string "remote" "get-url" remote)
                                (forge--split-forge-url $)))))
        (let ((old-target (magit-get-push-branch old t))
              (new-target (magit-get-push-branch new t))
              (remote (magit-get-push-remote new)))
          (when (and old-target
                     (not new-target)
                     (magit-y-or-n-p (format "Also rename %S to %S on \"%s\"?"
                                             old new remote)))
            ;; Rename on (i.e., within) the remote, but only if the
            ;; destination ref doesn't exist yet.  If that ref already
            ;; exists, then it probably is of some value and we better
            ;; not touch it.  Ignore what the local ref points at,
            ;; i.e., if the local and the remote ref didn't point at
            ;; the same commit before the rename then keep it that way.
            (magit-call-git "push" "-v" remote
                            (format "%s:refs/heads/%s" old-target new)
                            (format ":refs/heads/%s" old)))))))
  (magit-branch-unset-pushRemote old)
  (magit-refresh))