(defun my-magit-push-as-another-branch () "Recreate a Magit branch by deleting and creating a new one with branch name completion." (interactive) (let* ((branches (split-string (car (magit-git-items "for-each-ref" "--format=%(refname:short)" "refs/heads/")) "\n")) (branch-name (ivy-read "Enter branch name: " branches :require-match t :preselect (magit-get-current-branch) :sort nil)) (current-branch (magit-get-current-branch))) (when (yes-or-no-p (format "Recreate & push branch \"%s\"?" branch-name)) (magit-run-git "branch" "-D" branch-name)) (magit-run-git "checkout" "-b" branch-name current-branch) (magit-run-git "push" "--force" "origin" branch-name) (magit-checkout current-branch)))