Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why did Git create a merge commit with no file changes?

Tags:

git

git-merge

I am collaboratively working on a project with someone, so we decided to use git. Unfortunately, we frequently code in locations with no internet, so we end up with something like this:

origin/master: A---B---C                         \ mylocalmaster:           D---E---F                         \ hismaster:               G---H---I 

Now, say he pushes his commits and gets this:

origin/master: A---B---C---G---H---I                         \ master (local):          D---E---F 

All I want to do is push my commits to get this in both my local repo and the online one:

A---B---C---D---E---F---G---H---I 

It seems to work when I do git push, but the trouble arises when I do git fetch and then git merge. All I'm trying to do is get his commits into my local repo, but I end up with a merge commit saying something like Merge remote-tracking branch 'origin/master' as its message.

I don't want to have this pointless commit, since there is no conflicting code in our commits. We are working on completely different files, so there's no reason to have this commit. How can I prevent git from creating this merge commit?

like image 255
Alexis King Avatar asked Apr 14 '12 21:04

Alexis King


People also ask

Why is merge commit created?

Merge commits are unique against other commits in the fact that they have two parent commits. When creating a merge commit Git will attempt to auto magically merge the separate histories for you. If Git encounters a piece of data that is changed in both histories it will be unable to automatically combine them.

Does a merge create a new commit?

Merging your branch into master is the most common way to do this. Git creates a new commit (M) that is referred to as a merge commit that results from combining the changes from your feature branch and master from the point where the two branches diverged.

How do you stop git from merging?

You can use the git reset --merge command. You can also use the git merge --abort command.


2 Answers

You can omit creating merge commits, by using rebase instead of merge.

As @Dougal said, if you do git fetch, you can execute git rebase afterwards to change the base of your changes to the fetched HEAD.

Usually you create those unwanted merge commits, by pulling from remote repository. In that case you can add --rebase option:

git pull --rebase 

or add the proper option to Git config file (locally):

git config branch.<branch-name-here>.rebase true 

or for all the new repositories and branches:

git config branch.autosetuprebase always --global 

However, rebasing creates cleaner, more linear history it is good to create merge commits, where there are massive changes in both branches (use git merge to do so).

like image 127
Rafał Rawicki Avatar answered Oct 06 '22 00:10

Rafał Rawicki


Use git rebase (after a git fetch) to make your commits apply against his rather than against the previous master. That is, to go to ABCGHIDEF in your example. (You can't do ABCDEFGHI without having to do a push -f, because ABCGHI is already in origin/master and you'd have to override that.)

like image 42
Danica Avatar answered Oct 05 '22 23:10

Danica