Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why for git pull merging is the default behavior?

Usually when we update our local repository with new origin commits we do git fetch, git rebase. But we don't do git fetch git merge. Usually, the reason is that we don't want to add a new commit (merge commit) but just want to get all new commits on top of our code. So why then the default behavior for git pull is not with merge. I know that we can change it, but is there is better reason to use fetch-merge over fetch-rebase.

like image 267
Narek Avatar asked Oct 21 '15 11:10

Narek


People also ask

What should be the default Behaviour of git pull?

In its default mode, git pull is shorthand for git fetch followed by git merge FETCH_HEAD . More precisely, git pull runs git fetch with the given parameters and calls git merge to merge the retrieved branch heads into the current branch.

What is git default merge strategy?

Recursive is the default merge strategy when pulling or merging one branch. Additionally this can detect and handle merges involving renames, but currently cannot make use of detected copies. This is the default merge strategy when pulling or merging one branch.

Does git merge commit by default?

By default, Git does not create an extra merge commit when merging a commit that is a descendant of the current commit. Instead, the tip of the current branch is fast-forwarded.

Is git pull the same as merge?

A Git pull request is essentially the same as a Git merge request. Both requests achieve the same result: merging a developer's branch with the project's master or main branch. Their difference lies in which site they are used; GitHub uses the Git pull request, and GitLab uses the Git merge request.


2 Answers

I assume your question is why with git pull the default behavior is to merge instead of to rebase the pulled-in changes.

The answer is rather simple: Rebasing recreates the commits that are being rebased. Those new commit objects then replace the old ones. But those new commits are completely incompatible with the old ones. This causes all other users who have pulled the original commits before to have incompatible commits from the rebased ones you just recreated. So you have two conflicting versions with mostly the same change. This usually means a lot of problems since you need to fix these conflicts manually (by saying which of the two versions to keep; so if you rebased, the other ones all need to throw the old ones away—manually).

This is also why you should never rebase commits that have been published.

So the default solution is to merge, as merging is a completely safe operation that will not cause conflicts. Merge commits are just commits that are added to the history, but do not replace the existing history. Yes, it may make the history a bit more complex, but it’s completely transparent and compatible with all versions that existed before.

Of course, there are some situations in which rebasing is just fine. Most notably if you work locally on some unpublished changes and you want to update your repository. Then fetching and rebasing your local changes is fine since they only belong to yourself and nobody else knows about them yet. But this should always be an explicit action, so you don’t make any mistakes. And that’s why the rebasing pull is just an option: git pull -r.

like image 152
poke Avatar answered Nov 05 '22 15:11

poke


I think it works this way by default to avoid rewriting local history, and changing hashes of existing commits. These commits might be already pushed to the remote. The rebase is a destructive command, and pulling changes from the remote shouldn't destruct anything by default.

You can use git pull --rebase or just create an alias for this action.

like image 26
Stas Avatar answered Nov 05 '22 17:11

Stas