5

I am currently on master branch and I have to create new branch called test starting from master.

If I run b c in magit buffer, magit will prompt for starting point

Create branch starting at (default master):

How to prevent this and always use the current branch?

4
  • 1
    Are you sure you really need this? There is no such thing as default branch in git. Every branch is standard from technical point of view. This message in the minibuffer just tells you that if you press enter master branch will be used by default. This default may change depending on your previous choices. Always branching from master branch is not very good idea too. It may seem so if you work with simple pet projects, but once you work in a team, using something like git-flow, you will want to always explicitly choose Aug 31, 2015 at 7:34
  • branch to branch off from. Aug 31, 2015 at 7:34
  • @Mark Yes. I meant to use current branch. Aug 31, 2015 at 8:43
  • Oh, that makes more sense. Aug 31, 2015 at 8:46

3 Answers 3

5

The offered default is the current branch, therefore there already is a key binding for creating a new branch starting at the current branch. But that binding is b c RET, not b c.

Okay that was a slight simplification. If and only if there is a branch at point (e.g. in a log or the references buffer) then that branch is offered as the default instead.

3
  • in this context, can i bind c RET to c itself? Aug 31, 2015 at 12:15
  • No, but you can copy the definition of magit-branch, look at what happens inside magit-branch-read-args and do something similar inside the interactive of the copy. Then you have to bind that new command in the popup as described here. I.e. my claim that there is a key binding b c RET is not technically correct.
    – tarsius
    Aug 31, 2015 at 14:11
  • Thanks. I've added an example implementation along the lines you suggested. Nov 8, 2016 at 15:09
2

If you're running a Magit version newer than 2.8 (the relevant commit was on 2016-10-10), you can customize the user option magit-no-confirm-default.

If you add magit-branch-and-checkout to that list, then when you use bc you will only need to enter the new branch name, and Magit will use the default start point without prompting you.

n.b. That default start point is contextual! The prompt for the new branch will indicate what the start point will be.

e.g. If you are viewing a Magit Log or Magit Refs buffer, the start point would reflect the ref at point, rather than the branch you currently have checked out.

1

As @Tarsius mentions, you can define your own command to do this.

(defun wh/magit-branch-from-current-and-checkout (branch)
  "Create and checkout BRANCH from the current branch."
  (interactive (list (magit-read-string-ns "Branch name")))
  (let ((start-point (magit-get-current-branch)))
    (if (string-match-p "^stash@{[0-9]+}$" start-point)
        (magit-run-git "stash" "branch" branch start-point)
      (magit-call-git "checkout" "-b" branch start-point)
      (--when-let (and (magit-get-upstream-branch branch)
                       (magit-get-indirect-upstream-branch start-point))
        (magit-call-git "branch" (concat "--set-upstream-to=" it) branch))
      (magit-refresh))))

You can add this to the existing branch popup. In this case, I've chosen b f as the keybinding (as it's currently unused).

(magit-define-popup-action 'magit-branch-popup
  ?f "new branch From current" #'wh/magit-branch-from-current-and-checkout)

If you don't have unpushed commits on your current branch, you can also use b s (bound to magit-branch-spinoff) to create a new branch without prompting for the base branch.

Your Answer

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

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