Home > Software design >  Merging Git branches locally vs merging in remote repository?
Merging Git branches locally vs merging in remote repository?

Time:06-03

Scenario 1:

  • I have 'master' branch in both local and remote repositories
  • I create a 'feature-1' branch and make few commits in it.
  • Merge 'feature-1' branch in to 'master' branch
  • push changes to remote 'master' branch

Scenario 2:

  • I have 'master' & 'feature-1' branches in both local and remote repositories
  • I make few commits in local 'feature-1' branch
  • I push those changes to remote 'feature-1' branch
  • Merge 'feature-1' with 'master' on remote server (GitHub)

Both ways work. So under what circumstances is each approach used?

CodePudding user response:

Just to clarify:

The question is tagged as git. On those terms, scenario 2 does not exist. Git will absolutely refuse to perform a merge at the server side. It has no provision for such a thing; indeed, it is forbidden. That is why you cannot push to a branch that has commits you don't have locally.

However, some Git hosting sites, such as GitHub, have created a mechanism termed a "pull request" or "merge request" where it looks like a merge is being performed on the server side. This mechanism involves not only the final merge but the display (via a web browser) of the diff and the ability to comment upon it before the merge. This is to permit multiple collaborators to review one another's work on a branch before a merge takes place. Many workplaces install rules at GitHub saying that pushing a local merge commit to the main branch is forbidden; the merge must be performed by way of a pull/merge request, and the merge will be forbidden until the branch has been explicitly approved by a collaborator.

So the rule is simple:

  • If you are working alone, merge locally.

  • If you share this repo with other collaborators who will be wanting, independently, to create branches to be merged into the main branch, then by all means take advantage of pull/merge requests on the server side to facilitate collaboration, and configure rules to enforce whatever your policies are.

CodePudding user response:

The end result is effectively the same, however if you have to abide by a review process, you'll want to to push your feature branch and then create pull or merge request and then have it merged remotely after it was reviewed and approved.

CodePudding user response:

Simply scenario 2, because more colleagues get a chance to review it if needed, also to run any needed pipelines on push etc.

  •  Tags:  
  • git
  • Related