Search code examples
gitbranchpull

same git repo, 2 branches, pull to 1 branch does not include changes in other branches


I have 2 tasks to work on 2 source files in one git repo. There is no dependancy of these tasks, I can release either of them first. Given below situation and steps: git_repo/file1.json git_repo/file2.json

create 2 branches:

Command to commit change of file1.json

git checkout -b branch1
#do some change on file1.json
git add file1.json
git commit -m 'update file1.json'
git push -u origin branch1



Command to commit change of file2.json

git checkout -b branch2
#do some change on file2.json
git add file2.json
git commit -m 'update file2.json'
git push -u origin branch2

When I new a pull request from branch2 in GUI, expect: only file2.json will be included in the pull request actual: both changes of file1.json and file2.json are include in pull request.

How I can get my expecting result?


Solution

  • Unless there's some detail you've left out in your steps, it looks like this is what's happened:

    1. You created branch1 from master
    2. You added a commit to branch1
    3. You created branch2 from branch1
    4. You added a commit to branch2

    If that's the case, then the reason you see file1.json in your branch2..master diff is because of that third step: branch2 is based on, and therefore includes all the contents of, branch1.

    You should be able to fix this by running git checkout branch2 && git rebase master (or main, as appropriate). This will rework branch2 so that it's based on master (or main), instead of on branch1. (You'll also need to do a git push --force-with-lease when you push branch2, since the rebase will alter the HEAD of branch2.)