I think I have a fundamental [Git] misunderstanding, I don't know why a git merge didn't behave as I expected
#188656
-
Select Topic AreaQuestion BodyToday I finished making some changes to a Minimal API application and a Blazor application that consumes the API. I've spent a couple days working on this. In my own testing, it worked fine. So, I wanted to merge my child branch, which I'll just call "child", back into the main branch. And I also wanted to make sure that there would be as little for the reviewer to handle as possible, so I wanted to make sure I had all the code up to date, locally, before creating a PR to merge child into main. In the past I would create a PR, to go from main to my child branch. However, I've learned that the command However, it didn't go well at all. As I said I tested the application on my dev box, and it worked fine. After creating the PR to merge child into main, it complained about some code which wasn't even in my child branch! I spent 20 minutes trying to figure out where in heck that code came from. Some going over the sequence of steps taken, this is what happened starting late last week when I created the child branch:
Here's a code snippet to illustrate what the code looked like. Note, this isn't exact. I am home now and don't have access to my work GitHub environment. There was about 10 lines of code, at the bottom of a .razor file, which is now in the main branch, but that same .razor file in my child branch doesn't have that code at all. Here is sort of what it looks like: // Some C# code before this
var response = ApiCall.GetData("api/endpoint<APIResponse>/some-data"); // illustration of how the API is called
// Some C# code after thisThe It must be the case that between the time that I updated my local repo from origin, then created the child branch, that someone else had added that code assigning Anyway, I do not understand why the |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 9 replies
-
|
Welcome to the GitHub Community, @rfalanga, we're happy you're here! You are more likely to get a useful response if you are posting your question(s) in the applicable category. Check out this Guide on how to post in the right category. It'll ensure your questions and discussions reach the right people faster! This particular discussion belongs in the |
Beta Was this translation helpful? Give feedback.
-
|
Based on how I read this I'm assuming that you're not working on a forked repository, but you and the other team member(s) work on different branches in the same repository, is that correct? It's not relevant to the issue itself, but it might be relevant to how you investigate the issue. First thing I would probably do is check the output of I'd highly recommend trying out a Also, did that colleague merge your changes into Overall it seems like you did everything correctly. If you were working off of a fork, the issue might be because you merged |
Beta Was this translation helpful? Give feedback.
-
From your description, your workflow was: You switched to main and did git pull → local main was up-to-date. You created a child branch and started working. You finished coding and tested locally. You switched back to main and did another git pull → git said your local main was already up-to-date. You switched to child and did git merge main → this merged your local main into child. You created a PR → reviewer built the app → build failed, complaining about code that wasn’t in your child branch.
Your main misunderstanding is: “I thought git merge main would automatically bring all changes from main, including any remote commits, into my child branch so I could review them before creating a PR.” The reality is: When you created child, your local main was up-to-date at that moment. Someone else may have pushed new commits to the remote main after you created your child branch. When you ran git merge main on your child branch, Git only merged your local main, not the remote main. So your PR didn’t include the latest changes from remote main → reviewer’s build failed.
Before creating a PR, you should always sync your branch with remote main. For example: git checkout main Or in one step from child: git pull origin main # Pulls remote main directly into your child branch If there are conflicts, resolve them locally. Now your child branch contains all the latest changes from remote main, so your PR will be safer.
git merge main only merges your local main. It does not automatically include commits that exist on the remote main but not locally. Always run git fetch or git pull from remote main before merging into your feature branch. This ensures you see any potential issues (like your error) before creating the PR.
Your understanding wasn’t entirely wrong, but the missing piece is: merge doesn’t pull remote commits automatically. PR failures usually happen because your local main was outdated. Safe workflow: always update local main from remote before merging into your feature branch. |
Beta Was this translation helpful? Give feedback.
-
|
What you experienced is a fairly common Git workflow misunderstanding. The key issue is timing and synchronization between your local repository and the remote From the sequence you described, the most likely explanation is that new commits were pushed to Why
|
Beta Was this translation helpful? Give feedback.
Based on how I read this I'm assuming that you're not working on a forked repository, but you and the other team member(s) work on different branches in the same repository, is that correct? It's not relevant to the issue itself, but it might be relevant to how you investigate the issue.
First thing I would probably do is check the output of
git logon the repository'smainbranch. Where are your changes in the order of commits? Any changes applied after yours?I'd highly recommend trying out a
git bisect. The idea behindgit bisectis that you identify one commit as "good" and a later commit as "bad". In other words, you're saying "I know things were working here, and then they stopped w…