Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does git create merge commits? And how to remove these?

Tags:

git

github

A co-worker was working over a couple of weeks on a feature, and now the git commits look like:

colleag  add class  8b5bec5
colleag  add change 3535adc5
colleag  Merge branch 'NEW' of github.example.de:example/app into NEW   0cc2d24
colleag  add change
colleag  add validation     7eff440
colleag  rebase done wrong merge    f8e35e3
colleag  Merge branch 'NEW' of github.example.de:example/app into NEW   2168ac6
colleag  wrong merge    a6ed636
colleag  typo in spec   7b23633

I guess he is doing a:

git pull origin master

on his working branch.

So, my questions:

1) What does this mean:

Merge branch 'NEW' of github.example.de:example/app into NEW    2168ac6 

It is a merge commit, but why does git merge with remote here?

2) Is it possible to clean these merge commits? I am currently cherry-picking the commits, but not sure if this is best.

What else can be advised after reading the git log?

like image 206
poseid Avatar asked Oct 01 '13 13:10

poseid


1 Answers

Those commits are because your coworker had committed some work on their local branch and then pulled new commits from the remote. The key being that git pull is a combination of two git commands, git fetch and git merge. Generally these commits are fairly innocuous but in my experience they have occasionally caused some issues.

The commits can be avoided by running git pull --rebase origin master. This makes the pull a combination of git fetch and git rebase. This will set aside the local commits and bring in the commits from the remote then apply the local commits one at a time. As for the commits that you have already in your remote, you can try to clean them up with an interactive rebase but that will cause more trouble than it is worth. I would just try to change your coworkers work flow and leave those on the remote. Changing shared history is a risky and can be a painful process.

I wouldn't cherry-pick the merge commits if you just take commits that your coworker had you should be ok. Though generally, I prefer merging branches rather than trying to cherry-pick commits as it results in commits that have the same patch with different sha's and can cause merging branches to be more difficult than it should be.

like image 101
Schleis Avatar answered Sep 18 '22 21:09

Schleis