-2

My eyes were all over this page: Git merge without auto commit

I wanted to believe. But I tried and reality diverged.

I have this:

git fetch origin;
git merge --no-edit origin;

this seems to create new commits, even if there were no changes between the remote and the local branch.

I don't think that using

 git merge --no-ff origin;

or

git merge --no-commit origin;

are the answers. How can I merge the remote into the local without making a commit if there are no changes? Perhaps not do the merge at all, and first check to see if there is a difference?

What I am confused about: I assume --no-commit will break if the remote branch has changes that need to be merged into the local branch, because shouldn't that always require a new commit?

Alexander Mills
  • 90,741
  • 139
  • 482
  • 817
  • 1
    `--no-commit` leaves you in the middle of the merge. If you want to try updating without creating a merge commit *and fail otherwise*, the option is `--ff-only`. If `git merge --no-edit origin` doesn’t display “Already up to date”, then there *were* changes between the remote and the local branch (in the form of commits, not necessarily an actual content difference). – Ry- Sep 01 '18 at 00:14
  • Is the goal here something like `git pull --rebase`? I'm not impugning the question, just have limited knowledge and trying to follow along :) – zzxyz Sep 01 '18 at 00:23
  • yeah what I am looking for is no commit if there are no changes, and a normal commit if there are changes... – Alexander Mills Sep 01 '18 at 00:23
  • 1
    That’s what `git merge --no-edit origin` is. – Ry- Sep 01 '18 at 00:35
  • the problem, which is what the OP is talking about, is that in my testing `git merge --no-edit origin` produces new commits – Alexander Mills Sep 01 '18 at 01:28

1 Answers1

1

So as Ry- mentioned, if there were no changes in the remote branch, the output is Already up to date. and there is no commit. Therefore, I'm not sure if this addresses the behavior you consider problematic, but I suspect it might:

So say you have a local change and a remote change, and you pull the remote with git fetch git merge Your tree will look like this:

£ git log --oneline --graph --decorate
*   153b72e (HEAD -> master) Merge branch 'b2'
|\
| * eb48067 (b2) b2 mod
* | 6397e45 master mod
|/
* 034e374 initial

We have the two actual commits, and now a merge commit. A little messy for maybe a tiny config change checked in from a build machine or something.

So instead we can do it like this (instead of git merge):

£ git rebase b2
First, rewinding head to replay your work on top of it...
Applying: master mod
£ git log --oneline --graph --decorate
* 49de99c (HEAD -> master) master mod
* 5019681 (b2) b2 mod
* 1f8b73e initial

Aside from skipping the merge commit, this also skips identical commits. In other words, it's kind of the thing to do if the remote branch isn't really something you consider a branch (even though it technically is).

zzxyz
  • 2,953
  • 1
  • 16
  • 31