Home > database >  Two branches on Git are 1 commit off, but no differences in code
Two branches on Git are 1 commit off, but no differences in code

Time:11-10

I have two branches in my GitHub repository, dev and master. I was recently doing PR from dev into master and now they are 1 commit off.

master have 111 commits and dev have 112 commits, but both branches have exactly the same code.

This is annoying, because on GitHub, I see This branch is 1 commit ahead of dev. and when I do PR from master to dev, it is switched and I get This branch is 1 commit ahead of master.

How to fix this?

CodePudding user response:

I was recently doing PR from dev into master and now they are 1 commit off.

When you merged the PR, it probably created a "merge commit": a commit with two parents, which links the history of the two branches, like this:

              master before merge
                 v
... <--a <--b <--c
                   \
                    -- M <-- master after merge, pointing at merge commit
                   /
... <--x <--y <--z
                 ^
             dev, before and after merge

Unless you resolved any conflicts, this commit won't contain any changes, it just glues the history together. It's reachable from (i.e. is in the history of) "master", but not "dev", so it shows as a difference in their history.

You could merge back the other way, and then you'll have a new merge commit in the history of "dev" that's not in "master" (with "z" and "M" as parents). That might or might not be any less confusing depending on your workflow.

Or, you could "fast-forward" dev to point at the merge commit, but that will be fiddly if you've protected the branch to only be changed via Pull Request.

CodePudding user response:

If dev is supposed to be identical to master, you could force it into that state.

git checkout dev
git reset --hard master
git push --force

This will alter dev-- if there is a commit on that branch that you're not understanding, it will be gone. It doesn't seem like that's a concern from the question.

  • Related