3

I had something very strange happen the other day that I can't explain, that almost certainly results in a deficiency I have in understanding git and GitHub.

I'm working on a project on GitHub using the GitHub flow model. There were two feature branches being actively worked on - we'll call them A and B. Both were happily progressing with their own commits, until one commit on branch A that read:

Merge remote-tracking branch 'origin/b into b'

Ever since that commit, the pull request for branch A was showing the commits for both A and B. I didn't notice that, and when merging the pull request for A into master, it simultaneously closed the pull request for B in GitHub. Looking back at the pull request for B after it closed, it looked like it "lost" all of its commits except for two.

How did this happen? What git command(s) can link branches (and PRs) in this way? I'd like to be able to repro this starting with a clean repository as a learning example for the team.

1
  • 2
    You might have done git pull origin B while being on branch A some time in past.
    – Vishwanath
    Jul 19, 2015 at 4:16

1 Answer 1

0

How did commits from B get into A?

It sounds like you pulled or merged the changes from branch B into branch A, then pushed branch A (now containing the commits from both A and B) to GitHub. Here's a minimum viable example:

git clone <your repo clone url>; cd <reponame>
git commit --allow-empty -m "first commit guarantees shared history"
git checkout -b B
touch bar; git add bar; git commit -m "commit on branch B"
git push origin B
git checkout master
git checkout -b A
touch foo; git add foo; git commit -m "Commit on branch A"
git push origin A
# Create PR from A into master using GitHub UI
git status
On branch A
nothing to commit, working directory clean
git merge B
# your editor opens and you make a commit
git push origin A

Now all of the commits that were on branch B when you typed git merge B are merged into branch A. When you push them to branch A, they'll show up in the pull request from A to master, because GitHub assumes that subsequent commits to a branch are to address review feedback on a PR.

Why did the PR from B to master get closed?

Although you're probably used to merging PRs from the button in the UI, performing a merge on the command line does exactly the same thing. The way GitHub decides whether or not a PR was merged by hand is to compare the IDs of the commits in the PR to the IDs of the commits in the branch that you created the PR against (in this case, master). Since all of the commits from branch B were merged into master as part of the PR from branch A, it looked exactly the same to GitHub as if branch B had been merged by hand, so the PR was marked as 'merged' and no longer shows up as 'open'.

What git commands can "link" branches/PRs?

  • git merge otherbranch; git push remotename PRbranch
  • git pull remotename branchname; git push remotename PRbranch
  • git cherry-pick <commit range>; git push remotename PRbranch if you cherry-pick all the commits from some other PR
  • git apply <patchfile>; git push remotename PRbranch if for some reason the entire contents of another PR had been emailed to you as a patch

Basically, any command which brings commits from another branch into your history has the potential to cause the behavior that you saw.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.