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?

Drew
  • 75,699
  • 9
  • 109
  • 225
Chillar Anand
  • 4,042
  • 1
  • 23
  • 52
  • 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](http://nvie.com/posts/a-successful-git-branching-model/), you will want to always explicitly choose – Mark Karpov Aug 31 '15 at 07:34
  • branch to branch off from. – Mark Karpov Aug 31 '15 at 07:34
  • @Mark Yes. I meant to use current branch. – Chillar Anand Aug 31 '15 at 08:43
  • Oh, that makes more sense. – Mark Karpov Aug 31 '15 at 08:46

3 Answers3

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.

tarsius
  • 25,298
  • 4
  • 69
  • 109
  • in this context, can i bind `c RET` to `c` itself? – Chillar Anand Aug 31 '15 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](http://magit.vc/manual/magit-popup/Customizing-existing-popups.html). I.e. my claim that there is a key binding `b c RET` is not technically correct. – tarsius Aug 31 '15 at 14:11
  • Thanks. I've added an example implementation along the lines you suggested. – Wilfred Hughes Nov 08 '16 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.

phils
  • 48,657
  • 3
  • 76
  • 115
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.

Wilfred Hughes
  • 6,890
  • 2
  • 29
  • 59