0

Let's say I'm working on branch1 and create a branch2 from branch1.

Up until recently, when I did some changes to branch2 and didn't commit them without changing to branch1, the IntelliJ console warned me that I first need to commit my changes or I wouldn't be able to change.

But now, when using git checkout branch1 while I'm on branch2 without commiting my changes on branch2, for some reason all my changes get automatically commited AND transfered to branch1.

For example:

I'm on branch2 and modify some files. I forget to commit the changes and try to switch to branch1. This is what happens...

git checkout branch1
Switched to branch 'branch1'
M       src/main/java/Test.java
Your branch is up to date with 'origin/branch1'.

...and all the changes I made in branch2 are transfered to branch1!

I don't want this behaviour. I don't want to transfer all the changes I made in branch2 to branch1 when I'm using git checkout. It should warn me that I forgot to commit instead of adopting the changes.

EDIT:

Previously, I'd get an error message like the following one:

git checkout branch1
error: Your local changes to the following files would be overwritten by checkout:
    Test.java
Please, commit your changes or stash them before you can switch branches.
Aborting

And now the message is ignored and the changes are just automatically merged/transfered from branch2 to branch1 like in the example above, without showing me an error.

ful
  • 3
  • 2
  • 1
    You have not yet *committed* your new revision. Git carries it across the checkout if it can. See https://stackoverflow.com/q/22053757/1256452. Note that there is no flag to stop this happening; you just have to `git checkout` back, commit, and *then* `git checkout` the other branch. (IDEs may behave differently—this is all command-line oriented.) – torek May 01 '18 at 18:20
  • @torek Thanks, I'm aware of that. My problem is that I want git to warn me to commit instead of carrying the changes across. I've read the link you provided before, but still couldn't figure out how to fix the problem. – ful May 01 '18 at 18:23
  • 1
    I'd recommend using `git status` often, or perhaps even using one of those shell prompt-setting things that changes your prompt so that you know whether you have uncommitted work. (I have my own but there is a standard one for bash as part of the Git "contributed software" stuff.) There's no Git setting to make behave the way you want. – torek May 01 '18 at 18:27
  • @ful, this isn't a "problem". It's a deliberate choice made by Git's developers. Your uncommitted changes aren't being carried from one branch to another—they don't exist in _any_ branch. – ChrisGPT was on strike May 01 '18 at 18:50
  • @Chris, Huh, I'm really confused. My uncommitted changes are automatically carried over to the other branches for some reason now when I use git checkout. Previously, that wasn't the case and I had to commit them first, otherwise it wouldn't allow me to change my branch. Some settings must have been changed, since it behaves differently now. And I'm trying to figure out what exactly so I can understand it better. Maybe it's an IntelliJ problem? – ful May 01 '18 at 18:58
  • @ful, if changing branches would cause data loss you'll get a prompt like that. If changing branches doesn't cause conflicts there's no prompt. – ChrisGPT was on strike May 01 '18 at 19:00
  • @Chris, I've edited my post to further clarify my problem. Previously, I'd get the error message you're talking about. But now in every case, the error message is skipped and all the changes are automatically committed and carried over to the branch I want to switch to. – ful May 01 '18 at 19:13
  • All the changes are automatically _committed_? That would be _very_ strange, but it's not what your example shows. You're simply seeing uncommitted changes, which, again, _don't belong to **any** branch_. They're _not_ carried from one branch to another. They're _not in a branch to begin with_. Please read the question that torek posted in his first comment. He wrote the very detailed accepted answer, and it should clarify this for you. – ChrisGPT was on strike May 01 '18 at 19:16
  • @Chris, Thanks again for the help, you brought me to the right track. Alright, then my uncommitted changes are automatically getting transferred over to the new branch. I've read the entire answer at least 3 times, and I still couldn't figure out how to fix it though. Why does it stash the changes automatically? How do I deactivate that? I couldn't figure it out. – ful May 01 '18 at 19:26

2 Answers2

2

The changes you describe as "on branch2" are not actually on branch2. They are changes to your working set and have not been staged nor committed to any branch.

If your working set has changes, and you check out a branch, those changes remain in the working set if they do not conflict with the other branch.

To avoid this problem you can check for uncommitted changes before checking out a branch (I do git status -sb before any checkout). And even if you forgot to do this, you can always just return to branch2 with git checkout branch2 and then commit the changes.

M.M
  • 138,810
  • 21
  • 208
  • 365
1

That's default git behavior. When there are conflicts between uncommitted changes and the branch been checked out, git refuses to say you should commit changes first.

When there are no conflicts, uncommitted changes are left intact in the working tree. There is no way of changing this. You could read about it in details e.g. here

IntelliJ just handles the case offering you Smart checkout (Stash changes - Checkout - Unstash) when possible.

Dmitrii Smirnov
  • 7,073
  • 1
  • 19
  • 29