1

I have a tracked file (an old versioned file) that i modified on master branch. The staus on master branch shows this file as modified but The status on the topic branch also shows this file as modified.

Earlier there was an error "error: You have local changes to "X"; cannot switch branches" when i checkout to the other branch without doing a stash or without using "-f" in git-checkout.

i have checked "stackoverflow.com/questions/1304626/git-switch-branch-and-ignore-any-changes-without-committing" but that error doesnt occur and am able to checkout even when i have local changes.

Is there any kind of config that is causing this confusion?

Senthil A Kumar
  • 10,306
  • 15
  • 44
  • 55

3 Answers3

5

Unless you commit, the modified changes are not part of any branch but is applicable to your working directory and checking out another branch will bring those changes to the other branch as well ( unless there are conflicts)

If you don't want the changes you did while in master to be in other branch when check that out, either commit the changes or stash them.

manojlds
  • 290,304
  • 63
  • 469
  • 417
  • Thanks, but earlier there was a message like "error: You have local changes to "X"; cannot switch branches." now this doesn't show up, is there any changes? – Senthil A Kumar Nov 28 '11 at 06:48
  • @SenthilAKumar - Have a look here http://stackoverflow.com/questions/1304626/git-switch-branch-and-ignore-any-changes-without-committing – manojlds Nov 28 '11 at 06:55
  • it says "You need a clean state to change branches (only if the branch change affects the 'dirty files', as Charles Bailey remarks in the comments)" but even when i have dirty changes i can switch branch without -f option – Senthil A Kumar Nov 28 '11 at 07:10
  • 1
    That can happen if the branch you're switching to is in exactly the same state as the branch you're switching from. In other words, if the changes on the branch you're switching to are the same as the branch you're switching from, you can switch branches freely. A simple test of this is to create a new branch without switching to it, then change a tracked file. After that, try switching to the new branch. You will be able to. – Carl Nov 28 '11 at 17:56
2

It looks like the branch you're switching to is in the same state as the branch you're coming from. Each branch is a pointer on a tree of commits. Therefore, if two branches point to the same set of commits , you can switch between them without checking in local changes. The reason is that until you actually commit the changes, it does not make a difference to git which branch you commit them to. However, once you have commit the change, then one branch has it and the other one does not and their histories are now different.

Carl
  • 43,122
  • 10
  • 80
  • 104
0

try

git reset --hard HEAD

on the master

slashmili
  • 1,190
  • 13
  • 16
  • reset removes the changes on both branches. I want to know why GIT shows the same changes on both branches – Senthil A Kumar Nov 28 '11 at 05:48
  • if you run it on master, reset removes uncommitted changes only in master – slashmili Nov 28 '11 at 06:02
  • if as you said that changes didn't commit to other branch I guess you didn't commit changes in first branch and you just checkout to master without committing – slashmili Nov 28 '11 at 06:09