Alexis King Alexis King - 3 months ago 8
Git Question

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

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?

Answer

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).