Vishali Alahappan
Vishali Alahappan

Reputation: 11

How to point to a same commit in two branches in Github?

Assume I have three branches master,develop and release in remote repository.

  1. When a PR is raised against master branch (assume commit ID is ae3r4...), I can merge using one of the three options that Github provides
    • Create a merge commit
    • Squash and merge
    • Rebase and merge
  2. In all of the options a new merge commit is created and in the history of master commits, I see a new commit ID (233f1d...).
  3. When I raise a PR against develop from master, release from master a new commit will be again created in every one of these branches.

I don't want that. When I merge a new commit in master, I want the same commit to be present in develop and release. How do I do it? How is this generally done in organisations to point to the same commit from different branches, like a bash script or from the UI? Coz, whenever we merge a commit to master this has to happen, so it'd be helpful if the solutions takes into account the maintenance part too.

Upvotes: 1

Views: 1753

Answers (2)

KAnggara75
KAnggara75

Reputation: 113

two branches can point to the same commits. However in the case of dev the branches don't share the same history, so you have to tell Git it's okay to basically squash the existing dev branch.

This will point your dev branch to whatever your local master is pointed to:

git push origin +master:dev

Keep in mind, it will squash whatever dev used to point to on the server. The + sign tells Git it's okay to squish stuff.

Upvotes: 0

VonC
VonC

Reputation: 1329232

In all of the options a new merge commit is created

There is one option where potentially no new merge commit is created.

See "Rebasing and merging your commits"

When you select the Rebase and merge option on a pull request on GitHub, all commits from the topic branch (or head branch) are added onto the base branch individually without a merge commit.

Pull requests with rebased commits are merged using the fast-forward option.

That means: if you first rebase locally your PR branch on top of origin/master, and force push it, then:

  • a Rebase and merge will not generate a new commit.
  • your master HEAD and PR HEAD will be the same.

But that approach works for a topic branch, not for a branch used by many for integration (like "develop").
And that would be for PR from topic branches to master: it is not a best practice to merge master to other branches (PR or not).

That is why I recommend the use of gitworkflow (one word): see more at rocketraman/gitworkflow.

Upvotes: 1

Related Questions