How do two branches become "linked" in git?

I had something very strange the other day that I can't explain, which almost certainly leads to a flaw I have in my understanding of git and GitHub.

I am working on a project on GitHub using the GitHub model . There were two feature branches heavily involved - we'll call them A and B. Both were happily evolved with their own commits until they were committed to branch A which read:

Merging remote tracking branch 'origin / b to b'

Since the commit, the pull request for branch A shows commits for A and B. I didn't notice that, and when merging the pull request for A to master, it was closing the pull request for B in GitHub at the same time. Looking back at the pull request for B after it closed, it looked like it had "lost" all of its commits except for two.

How did this happen? What git commands can link branches (and PRs) this way? I would like this method to start with a clean repository as a tutorial for the team.

+3


source to share


1 answer


How did you go from B to A?

It looks like you pulled or merged changes from branch B to branch A, and then dragged branch A (now containing commits from A and B) to GitHub. Here's a minimal 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 the commits that were on branch B when you git merge B

check in are merged into branch A. When you push them to branch A, they will appear in the pull request from A to master, because GitHub assumes that a subsequent commit in the branch is for feedback consideration on dealing with OR.

Why did PR from B to host shut down?



While you're probably used to merging PR from a button in the UI, doing the merge at the command line does the same thing. The way GitHub decides if a PR has been merged manually is to compare the commit IDs in the PR with the commit IDs on the branches you created the PR against (in this case, master). Since all commits from branch B were merged into master as part of the PR from branch A, it looked exactly like GitHub, as if branch B had been merged manually, so the PR was marked as "merged" and no longer displayed 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 commits from another PR
  • git apply <patchfile>; git push remotename PRbranch

    if for some reason all the content of the other PR was sent to you as a patch

Basically, any command that brings commits from another branch into your history can trigger the behavior you saw.

0


source







All Articles