0

I have created a branch named test based on the origin/master branch, then I made some changes to the test branch, I found out the same modification also appeared on the local master branch, is it normal?

Or What should I do to avoid this situation happen again,should I create a branch base on another remote branch?

Thanks for your answer

Ôrel
  • 7,044
  • 3
  • 27
  • 46
AutumnZeng
  • 49
  • 4
  • 3
    It sounds like you didn't checkout or switch to the new branch before committing. If you had the master would not have been affected – evolutionxbox Aug 16 '22 at 09:56
  • Time to check the branch that you have checked out with `git status` or `git btranch`. – eftshift0 Aug 16 '22 at 10:11
  • 1
    Can you share how you have created your test branch, commit change to it and switch back to master ? – Ôrel Aug 16 '22 at 10:20
  • My git command is `git checkout -b test origin/master`, I haven't commit the modification yet – AutumnZeng Aug 16 '22 at 11:10
  • 3
    If you haven't committed, then the claim "the same modification also appeared on the local master branch" is false. Your "modification" is not "on" _any_ branch. It's just some stuff hanging around in your working tree. – matt Aug 16 '22 at 11:33
  • 1
    Probably a duplicate of https://stackoverflow.com/questions/67450004/git-checkout-branch-after-git-add-seems-not-update-index-and-working-area See my answer there, which explains why uncommitted changes remain in the working tree when you switch branches. – matt Aug 16 '22 at 11:37

1 Answers1

2

My git command is git checkout -b test origin/master

Do not use the old and confusing command git checkout, but, since Git 2.23 (Q3 2019) the git switch command.

Any modified files in the index (git add) would still be there when switching branch. If you don't want that:

  • either git switch to your previous branch (git switch -) and commit there.
  • or git stash those changes.
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250