0

I have created two branches for a project I'm working on with git in IntelliJ. The first branch (A) I was working on modifying a file (call it A.Java). When I switched to second branch (B) with IntelliJ checkout, after modifying A.java under branch A, the file resulted to have the same modification in B that I was doing only under A!

This policy doesn't seems normal to me, since when I'm working in A I don't want to affect other branches. Where is the problem? Is a git behavior or it is only in IntelliJ?

Lore
  • 1,286
  • 1
  • 22
  • 57
  • 1
    Did you commit the file before you checked out branch B? – Oliver Charlesworth Mar 07 '18 at 15:37
  • I retire my previous comment: I had a problem with previous commit and wasn't executed. When I commit and push on the right branch this behavior disappear. But I think is not very safe behavior. It exists a "strict mode" to avoid switching branches even if you haven't committed? – Lore Mar 07 '18 at 15:42

1 Answers1

1

If you did not commit the changes on branch A before checking out branch B then that behavior is normal. If you did commit the changes on branch A then created and switched to branch B then the changes should also show up in B (eg. you did git checkout b -b)

Arnaud VdP
  • 227
  • 1
  • 9
  • When I commit and push on the right branch this behavior disappear. But I think is not very safe behavior. It exists a "strict mode" to avoid switching branches even if you haven't committed? – Lore Mar 07 '18 at 15:45
  • 1
    I found [this](https://stackoverflow.com/questions/8526279/git-allows-for-branch-change-with-unstaged-changes) post from a while ago that might clarify – Arnaud VdP Mar 07 '18 at 15:56
  • So as I understand, to avoid this behavior it is sufficient just to do one commit just after creating branch, right? – Lore Mar 07 '18 at 15:59
  • No. Doing a commit just after creating a branch does not affect this behavior. – yole Mar 07 '18 at 16:09
  • If I do an initial commit in a brand new branch, then trying to switch to another branch, Intellij blocks me. So it works. – Lore Mar 07 '18 at 16:11