0

If I have the typical scenario of a feature branch I'm committing to locally and the remote master branch changes during this time, I like to rebase my feature branch before merging it back into master.

Is there a way to configure the Git repo so that it detects if my feature branch and master branch don't have the same history (i.e. - I didn't rebase) and it will automatically reject my attempt to merge the feature branch back into the master branch?

Thanks!

Hoonerbean
  • 367
  • 1
  • 4
  • 12
  • If you create your `feature` branch from `master`, then it will anyway have common history. So check of same history will not work. Even without rebase, you will be able to merge it (just it may happen that there will be merge conflicts). Check these threads: https://stackoverflow.com/questions/53735519/force-rebase-merge-master-to-feature-branch-before-pull-request, https://stackoverflow.com/questions/44500174/force-branch-to-be-rebased-before-it-is-merged-and-pushed, https://stackoverflow.com/questions/32657757/force-feature-branch-to-be-rebased-before-it-is-merged-or-pushed, etc. – kosist Jan 23 '23 at 15:26

1 Answers1

0

You want to use git merge --ff-only, this will only use a fast-forward merge and if it isn't able to do that it will abort the merge.

https://git-scm.com/docs/git-merge#Documentation/git-merge.txt---ff-only

I don't think that you are able to set this as the default action for the repo but you could create an alias that does this so that you don't have to remember to do the flag. https://git-scm.com/book/en/v2/Git-Basics-Git-Aliases

This answer has a good explanation of the fast-foward merge. https://stackoverflow.com/a/6701322/498699

Schleis
  • 41,516
  • 7
  • 68
  • 87
  • Thanks @Schleis. I think this is the right track. I haven't investigated hooks yet, but maybe something can be done there with that. Your answer also led me to this post (https://stackoverflow.com/a/49272912/1663208) about how I can just check to see if the histories are the same. Maybe I can use that in a script with Git hooks too. Thanks! – Hoonerbean Jan 23 '23 at 17:42