5

Is there a way to prevent a silly mistake such as rebasing master onto another branch?

It might be possible to undo this by using the reflog, but I would like to avoid the hassle by preventing the rebase in the first place.

HRJ
  • 17,079
  • 11
  • 56
  • 80
  • Why would that be a silly mistake? Rebasing eliminates needless merge commits when keeping a feature branch up-to-date with the latest changes from master. – Makoto Jul 17 '15 at 03:08
  • Your language is a bit off. Typically _feature_ branches are rebased on _master_, and not something else. As @Makoto said, why do you want to prevent a core Git functionality from happening? – Tim Biegeleisen Jul 17 '15 at 03:12
  • Can you tell us more about your setup? Are you using GitHub, BitBucket, something else? – Tim Biegeleisen Jul 17 '15 at 03:14
  • 3
    @Makoto, Tim I am trying to prevent a rebase of 'master' and not that of a feature branch, precisely because master is not a feature-branch. I am not sure which part of that needs rewording / clarification. – HRJ Jul 17 '15 at 05:39

2 Answers2

9

This gist shows how to use a pre-rebase hook to avoid git rebases the way you want.

https://gist.github.com/uasi/9384329

You would just have to previously configure which branches you want to avoid rebasing through git config

iurifq
  • 116
  • 3
3

There is a pre-rebase git hook:

pre-rebase
   This hook is called by git rebase and can be used to prevent a branch
   from getting rebased. The hook may be called with one or two
   parameters. The first parameter is the upstream from which the series
   was forked. The second parameter is the branch being rebased, and is
   not set when rebasing the current branch.

You could probably use this to implement the functionality you're asking about.

larsks
  • 277,717
  • 41
  • 399
  • 399
  • Thanks! I had not looked into hooks because they can't be set globally. I was hoping for something like a config option, or an alias. Will fall back on this if I can't find anything else. – HRJ Jul 17 '15 at 05:49
  • @HRJ there is now a way to set global git hooks - see https://stackoverflow.com/a/37293001/9654314 – tilde Oct 28 '20 at 16:36