Home > database >  Why does my develop branch make me commit a merge every time?
Why does my develop branch make me commit a merge every time?

Time:10-02

For some reason every time I do a git pull on my local develop branch it forces a new merge commit, creating a hideous monster in my PRs with many of these messages. How do I stop this?

Merge branch 'develop' of github.com:repo into develop

enter image description here

CodePudding user response:

Presumably you have pull configured to merge, which is the default.

If you run git fetch instead if git pull, do you see updates on origin/develop? How would you like those updates integrated into your local develop branch?

The options are

  1. merge (the default)
  2. rebase (which re-applies your local changes on top of the remote ones)

You can't really use rebase if you've pushed your local develop branch to any remote (unless you want to force-push updates), but otherwise it's fine. Test it by running

git pull --rebase

explicitly, and set pull.rebase if you like this behaviour and want to make it the default.


NB. I'd always recommend running fetch first anyway, so you can check whether you actually want to integrate all the upstream changes immediately. Then again, if you develop on a feature branch instead of the shared develop, this really stops being such a problem.

CodePudding user response:

Pulling and pushing are all about sharing commits to make sure both sides have exactly the same set of commits.

When you pull, you will get a merge commit if:

  • You have commits on this branch that the remote does not have.

  • Or, the remote has commits on this branch that you do not have, and your pull.ff config (or perhaps merge.ff? I'm always a bit fuzzy on this) is set to false.

You can learn what the situation is by saying git fetch and then git status. Git will tell you who is behind and who is ahead. "Ahead" simply means "has commits that the other side does not".

If the remote is ahead and you are not, you can say git pull --ff-only to force a fast-forward and no merge commit.

Even if you are both ahead of and behind the remote, you can avoid the merge commit by saying git pull --rebase; this will pull down the commits from the remote that you do not have, and will then try to line up your commits that the remote does not have after those commits. But don't do that if you have already pushed this branch to form a pull request, as that would arguably constitute rewriting shared history, which is a no-no.

  • Related