1

I'm telling my developers don't use git merge command instead of that use git rebase. My requirement is if a user/developer accidentally hits git merge command i want disable the git merge (command) from git always.

Is that Possible to achieve This?

Or if the developer hits git merge it will perform rebase.

G.kalyan
  • 31
  • 6
  • 6
    Do you have an SVN background? Or why do you insist on always rebase? With `git`, merge is the much less problematic approach: A rebase creates new commits that have never been tested, while a merge records history the way it happened, giving much more utility to tools like `git bisect`. You really don't want to avoid merges with `git`. – cmaster - reinstate monica Sep 09 '20 at 10:03
  • You can check in a server-side hook like `pre-receive` if the new commits include any merge commit. If any, fail the push and tell the user to use `git pull -r` to flatten the branch. – ElpieKay Sep 09 '20 at 10:14

2 Answers2

1

In addition of a pre-receive hook which detect any merge commit, you can also request for your team to use, with Git 2.6+:

git config --global pull.rebase true
git config --global  rebase.autoStash true

Any git pull would actually be a git pull --rebase, forcing them to always replay their local (yet to be pushed) commits on top of an updated remote tracking branch.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • If a developer fire git merge branch_name i dont want to execute that command and given an error message like git rebase only is that really possible ? – G.kalyan Sep 14 '20 at 12:57
  • @G.kalyan Yes, either though pre-receive hook on the server side, or through a prepare-commit-msg hook on the client side (https://stackoverflow.com/a/19460322/6309) – VonC Sep 14 '20 at 13:08
  • can u please share the script or code of pre-receive hook it really helpful for me Thanks – G.kalyan Sep 15 '20 at 04:52
  • @G.kalyan That code would follow what is presented in the first link of the answer: https://stackoverflow.com/a/33248257/6309 – VonC Sep 15 '20 at 05:35
0

This option (git config --global pull.rebase true) will only work when I am trying to pull the changes from remote. But this doesn't work when I merging one local branch onto another. What is best way to disable merge command in git locally and globally both ?

  • 1
    This is not an answer but rather a question. Maybe it makes sense to ask this as a separate question or as a comment to the answer above – papanito Sep 16 '20 at 06:49