I'm thinking about automating my following git workflow in emacs:

  1. checkout to a new branch
  2. remove some pattern in the last commit message
  3. commit (amend) it
  4. push it to upstream

How could I automate 4 steps in emacs?

And I've tried to combine step 2 and step 3 into one key in magit by:

(defun magit-commit-append-new-msg ()
  (let* ((git-dir (magit-toplevel))
    ;; get last commit msg
    ;; TODO handle remote dir
    (msg (shell-command-to-string (format "git -C \"%s\" log -1 --format=\"%%B\"" git-dir)))
    ;; remove changeid and jira line in msg
    (msg-new (concat msg "[MYTEST]")))
    (magit-git-command-topdir (format "git -C \"%s\" commit --amend -m \"%s\"" git-dir msg-new))))

(magit-define-popup-action 'magit-commit-popup
  "Commit with new msg"

But I've not found a way do all 4 steps yet.

  • Why do you have to create and checkout a new branch? Am I right in assuming that you are on a branch like new-feature and want your modified commit to be pushed to origin/new-feature?
    – tarsius
    Commented Jun 3, 2018 at 4:43
  • @tarsius The reason I have to checkout a new branch is due to our work process, we have a branch for internal review and before it is pushed to outside we must remove some internal used information from the reviewed commit.
    – zwy
    Commented Jun 4, 2018 at 12:44

1 Answer 1


Something like this might work.

(defun my-fixup-head-message-and-publish (remote branch)
   ;; TODO I don't know your needs you should probably adjust this.
   (list "public"
         (concat "publish/" (magit-get-current-branch))))
    (magit-call-git "branch" branch (magit-get-current-branch))
    (magit-call-git "commit" "--amend" "-m"
                    (concat "[MYTEST]"
                            (magit-rev-format "%B")))
    (magit-call-git "push" branch (concat remote "/" branch)

You are probably better off asking "please implement this for me" questions on r/emacs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.