6

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 Commented Aug 31, 2015 at 7:34
  • branch to branch off from. Commented Aug 31, 2015 at 7:34
  • @Mark Yes. I meant to use current branch. Commented Aug 31, 2015 at 8:43
  • Oh, that makes more sense. Commented Aug 31, 2015 at 8:46

3 Answers 3

6

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? Commented 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
    Commented Aug 31, 2015 at 14:11
  • Thanks. I've added an example implementation along the lines you suggested. Commented 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.

10
  • I have magit 3.3 and the option magit-no-confirm-default is not available. Commented Nov 17, 2023 at 11:55
  • Search the release notes and you'll find that in v2.12: "The option magit-no-confirm-default has been demoted to a variable and will be removed in v2.13.0. Please migrate your customizations to the new option magit-dwim-selection."
    – phils
    Commented Nov 17, 2023 at 12:21
  • But the magit-dwim-selection customization does not offer settings for branch creation. Also, nearly everything is already set to "use default without confirmation". Commented Nov 19, 2023 at 16:01
  • You're not limited to the commands which are included in that option by default. Simply add the command magit-branch-and-checkout and "use default without confirmation" for the prompt regexp ^Create and checkout branch starting at.
    – phils
    Commented Nov 19, 2023 at 20:30
  • Sadly that does not work,magit still asks for the starting branch. Commented Nov 21, 2023 at 14:57
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 you have read our privacy policy.

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