How to prevent Gitlab from creating extra merge commit on branch merge

匆匆过客 提交于 2020-01-12 14:56:31

问题


I use GitLab in my project. I'm exploring Merge Requests feature.

  1. I created a topic_branch from master.
  2. Made a bunch of commits on topic_branch.
  3. Pushed topic_branch to remote.
  4. Created a merge request on master to pull changes from topic_branch.
  5. On accept merge in Gitlab, master pulled all the commits and also created a merge commit which is horrible to see duplication of code.

I should have created a squash of commits on branch and then created merge request. But still master would have two new commits, one from the branch and another one would be merge commit. I'm assuming, if I do this from command line i.e,

  1. checkout master
  2. merge topic_branch into master
  3. commit / push master In this case, there would be only 1 commit on master.

How to achieve this from GitLab ?


回答1:


TL;DR

Your Project > Settings > General > Merge Request Settings > Fast-Forward Merge.

Personally, I also prefer to do squash commits on every merge.


I think GitLab supports this now.

Steps

  1. Go to your project
  2. Navigate to project settings (Note: This is NOT the profile settings available on the top right)
  3. Go to General tab.
  4. Navigate to Merge Request Settings section
  5. Choose Fast-Forward Merge.




回答2:


As I understood from http://doc.gitlab.com/ee/workflow/rebase_before_merge.html GitLab will always create merge commit to keep a way to revert the whole branch.




回答3:


  1. checkout master
  2. merge topic_branch into master
  3. commit / push master In this case, there would be only 1 commit on master.

That is not true. You will have all the commits from topic_branch and a Merge branch 'topic_branch'commit in your master, except the case that no commits were added to your master branch since you branched off topic_branch. In this case, the default behaviour of git merge is to perform a fast forward merge. A fast forward merge merges your topic_branches changes to master without a merge commit (see here for further documentation).

However, when you create a merge request in GitLab, as the name says, you are requesting to merge your topic_branch into your code base. Per default, GitLab will always create a merge request, even if a fast forward merge is possible, to preserve the fact that the commits were developed on another branch in your history.

Now the good news: you can configure GitLab to perform fast-forward merges instead of creating merge commits: see here. It seems, however, that this is only possible in GitLab Enterprise Edition.




回答4:


update currently gitlab supports both rejecting non-fast-forward commits and squashing multiple commits of a merge request, so my comment below isn't that valid anymore

original content

If you don't want to have a merge commit, don't do a merge. That means do a rebase on target branch (master) and a push.



来源:https://stackoverflow.com/questions/29434100/how-to-prevent-gitlab-from-creating-extra-merge-commit-on-branch-merge

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!