How to make magit pull to have rebase option by default?

2k Views Asked by At

I recently started using magit package in emacs and its really useful and handy. I would like to make couple of tweaks.

How do I make magit pull to be always have rebase option?. Currently it shows up switches and I have to choose -r --rebase. In my dev process, we always rebase.

I use gtags+global for code browsing. Upon magit pull, It would be great if I can have a hook to rebuild the gtags again. Is there hook after git pull success ?.

3

There are 3 best solutions below

4
AudioBubble On

I do not know any Magit-specific setting, but you can just configure git itself accordingly:

git config --global pull.rebase true

I'd strictly advise against on enabling this setting globally.

As I have said in my comment, you should really change your development process so that there is no need for merging/rebasing on git pull. Don't work on shared branches, but instead always create your own feature branches for your work, and explicitly merge when needed.

You can then explicitly configure individual shared branches in your repository for rebasing, e.g.

git config --local branch.my-fancy-feature.rebase true

As for the hook, there is no specific hook for git pull. However, there is no harm in updating the tags file on other changes to the tree as well, so you may just the post-rewrite (for git rebase) and post-merge (for git merge) hooks.

See Tim Pope's post Effortless Ctags with Git for details on such a setup.

0
AudioBubble On

Add the following to your .emacs:

(defun magit-key-mode--add-default-options (arguments)
  (if (eq (car arguments) 'pulling)
      (list 'pulling (list "--rebase"))
    arguments))
(advice-add 'magit-key-mode :filter-args #'magit-key-mode--add-default-options)
0
AudioBubble On

This code will do what you want and make it easy to add default options for more modes:

(defun magit-key-mode--add-default-options (arguments)
  (let* ((mode (car arguments))
         (options (cadr arguments))
         (default-options (cdr (assoc mode my/magit-default-options))))
    (list mode (delete-dups (delq nil (append options default-options))))))

(setq my/magit-default-options
      `(
        (pulling "--rebase")
        ))

(advice-add 'magit-key-mode :filter-args #'magit-key-mode--add-default-options)

Note: This should be an edit to doublep's answer, but 3-out-of-4 wonks rejected the edit. Not one of them has even touched Emacs.SE, nor do they have any mention of Emacs or Lisp on their SO or LinkedIn profiles. Clearly, people who don't know the language in question should not be endowed with the power to reject edits.

I came here looking for an answer to this question, found doublep's answer, improved it by making the code clearer and extensible, and spent my own valuable time contributing the improvements back to the community, only to have it shot down by people who don't even know what they're doing. Every time this happens (and, so far, it has happened every time), I wonder why I'm wasting my time contributing to this site.