admin管理员组

文章数量:1389890

I have an automation the needs to create a Pull Request which (once merged) forces master to perfectly match dev.

Doing a simple Pull Request from dev->master seems right, but that brings dev's "new" commits to master. master will still maintain any "new" commits of its own. The branches may end up not matching, or the PR may cause a merge conflict.

A --force push of dev->master works, but those aren't possible in a Pull Request.

How can I create a Pull Request that forces master to exactly match dev and without merge conflicts?

(I'm answering below but hoping one of you git ninjas will confirm or tell me if there's a better way)

I have an automation the needs to create a Pull Request which (once merged) forces master to perfectly match dev.

Doing a simple Pull Request from dev->master seems right, but that brings dev's "new" commits to master. master will still maintain any "new" commits of its own. The branches may end up not matching, or the PR may cause a merge conflict.

A --force push of dev->master works, but those aren't possible in a Pull Request.

How can I create a Pull Request that forces master to exactly match dev and without merge conflicts?

(I'm answering below but hoping one of you git ninjas will confirm or tell me if there's a better way)

Share Improve this question asked Mar 14 at 20:09 bendytreebendytree 13.6k12 gold badges80 silver badges96 bronze badges 1
  • My gut feeling is "there's a better way". Out of curiosity, what's the end goal here? It seems like you're willing to throw away the extra commits (or at least the changes) on master that aren't in dev yet. So why do you regularly have those commits on master if you wish to automate ignoring them? What does your master and dev branch represent in your work flow? – TTT Commented Mar 17 at 14:06
Add a comment  | 

2 Answers 2

Reset to default 2

If you mean to make master look like in content what dev looks like, you need to play a little bit with history... this should work:

git checkout dev
git merge -s ours master # merge master into dev.... this will *link* them in the new dev commit *but* actually dev won't change its content

If you create a PR to merge dev into master right now and merge it, master will end up having exactly the same content of dev.

Pull requests apply new commits. So it needs to see a commit which takes master directly from its current state to dev.

git checkout --no-checkout master
git checkout -B premaster # our starting point is master
git checkout dev -- . # our goal is for the file system to match "dev"
git commit -am "dev-to-master commit"
git push -f origin premaster # ignore any previous history to premaster

Now create pull request with head=premaster and base=master

If this PR is merged immediately then dev will match master. But if master receives any new commits before the merge, then those new commits will be retained and could cause a merge conflict.

本文标签: githubHow to force Git branches to match via a PRStack Overflow