Search code examples
gitbranch

Changes were detected in the master branch to work done in the sub-branch


I am using the master branch, and I created a new example branch and checked out.

After working in the example branch, I checked out to the master branch before committing, and the contents I am working on in the example branch are displayed as changed in the master branch.

As far as I know it doesn't affect different brunches.

What could have gone wrong?


Solution

  • If I understand your description correctly, nothing has gone wrong. You have made some changes which are not committed anywhere, and git is telling you what those uncommitted changes are.

    I suspect you have some misunderstandings about how git works, so let's step through your description, re-wording to a more "git's eye view" of what you did:

    I am using the master branch

    You set your currently checked out branch to be "master", and checked out the current commit pointed at by that branch

    ... and I created a new example branch

    You created a new branch pointer, named "example", pointing at the current commit

    ... and checked out

    You set the currently checked out branch to be "example" (which is currently pointing at the same commit as "master")

    After working in the example branch ... before committing

    You made some changes in your working copy, but didn't commit them. These changes don't belong to any branch, they're just sitting waiting to be committed.

    If you committed them at this stage, the "example" branch would point at your new commit. Since you didn't commit them, nothing happened to any branches.

    I checked out to the master branch

    You set the currently checked out branch to be "master". Since you haven't added any commits to either branch, this is still pointing at the same commit as "example", so no changes need to be checked out.

    the contents I am working on in the example branch are displayed as changed

    The changes you made to your working copy are still uncommitted. Git won't clear them away unless you explicitly ask it to, because that would mean losing your uncommitted work.

    ... in the master branch

    Your currently checked out branch is "master", which just means that if you commit some changes now, that branch will point at the new commit. If you don't commit, there is still nothing changed on either branch.