{"thread":{"id":"22024","subject":"You have local changes; cannot switch branches error question","startedAt":"2009-12-24T22:25:37Z","lastAt":"2009-12-25T14:32:03Z","messageCount":4,"participants":["Eugene Sajine","Nanako Shiraishi","Junio C Hamano","Matthieu Moy"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"130319","messageId":"76c5b8580912241425k741b24e9ie8ed92ca7cb8a659@mail.gmail.com","threadId":"22024","inReplyTo":null,"subject":"You have local changes; cannot switch branches error question","fromName":"Eugene Sajine","fromEmail":"euguess@gmail.com","sentAt":"2009-12-24T22:25:37Z","receivedAt":"2009-12-24T22:25:37Z","isPatch":false,"sender":{"key":"euguess@gmail.com","avatar":null},"body":"First of all Merry Christmas and Happy New Year to everybody! I wish\nGit to move to global dominance during next year even faster then it\ndoes now;)\n\nMy question is related to the fact that most of the time when i have\nsome local file changes Git is switching branches OK. But from time to\ntime in some repos I'm hitting this error and I cannot figure out why\nit suddenly starts to complain about it?\n\nFor example in one of my repos I somehow got to a state when I have\nthree files modified, and when I'm trying to switch branches it\ncomplains about one of them being changed locally, so it refuses to\nswitch branch\n\nOTOH i have tried many different scenarios in another repo to leave\nsome uncommitted staged or not staged  changes and switch branches and\nsame version of git does that just fine. (v 1.6.5.6).\n\nSo, what is correct behavior - do not switch or switch branches when\nthere are uncommitted changes?\n\nThanks,\nEugene\n"},{"id":"130320","messageId":"20091225075952.6117@nanako3.lavabit.com","threadId":"22024","inReplyTo":"76c5b8580912241425k741b24e9ie8ed92ca7cb8a659@mail.gmail.com","subject":"Re: You have local changes; cannot switch branches error question","fromName":"Nanako Shiraishi","fromEmail":"nanako3@lavabit.com","sentAt":"2009-12-24T22:59:52Z","receivedAt":"2009-12-24T22:59:52Z","isPatch":false,"sender":{"key":"nanako3@lavabit.com","avatar":"https://gravatar.com/avatar/3777b9e201c5883a62b1a6fdf7c53f2d712d1d80989146063ea861e33aad72a8?d=mp&s=160"},"body":"Quoting Eugene Sajine <euguess@gmail.com>\n\n> My question is related to the fact that most of the time when i have\n> some local file changes Git is switching branches OK. But from time to\n> time in some repos I'm hitting this error and I cannot figure out why\n> it suddenly starts to complain about it?\n\nThe command is telling you that it refrained to overwrite \nyour local changes. It's not complaining.  There is an \nexcellent summary of what switching branches with 'git \ncheckout' while you have local changes in your working tree.\n\nhttp://thread.gmane.org/gmane.comp.version-control.git/77700/focus=77708\n\n-- \nNanako Shiraishi\nhttp://ivory.ap.teacup.com/nanako3/\n"},{"id":"130321","messageId":"7vhbrghrsn.fsf@alter.siamese.dyndns.org","threadId":"22024","inReplyTo":"76c5b8580912241425k741b24e9ie8ed92ca7cb8a659@mail.gmail.com","subject":"Re: You have local changes; cannot switch branches error question","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2009-12-24T23:32:24Z","receivedAt":"2009-12-24T23:32:24Z","isPatch":false,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Eugene Sajine <euguess@gmail.com> writes:\n\n> For example in one of my repos I somehow got to a state when I have\n> three files modified, and when I'm trying to switch branches it\n> complains about one of them being changed locally, so it refuses to\n> switch branch\n\nSuppose you have a history like this:\n\n         o---B side\n        /\n    ---o----o----A master\n\nand both branches 'master' and 'side' have file, F and G.  G is different\nbetween commits A and B while F is the same between these two commits.\n\nYou have 'master' checked out, and modified both F and G.  This table\nshows the contents you have in each path:\n\n    path    master      work tree    side\n    -----------------------------------------\n       F    F1          F2           F1\n       G    G1          G2           G3\n\nIn git, your local modification does _not_ belong to any branch.  You need\nto commit them if you want to make them part of the history of your\ncurrent branch.  Checking out a different branch means you switch to the\nbranch and carry these changes along with you.  But it is not necessarily\nbe possible to do so without modifying what is in your work tree.\n\nFor path F, your local change is (F2-F1), and switching to branch 'side'\nmeans you would transplant that change on top of what that branch has,\nwhich happens to be F1.  That means the result is F2 (= F1 + (F2-F1)).\nIOW, because F is identical between master and side, the file in your work\ntree can stay the same.\n\nNow, think what should happen to path G.  The local change is (G2-G1), and\nyou need to transplant that change on top of G3, that is different from\nG1.  This computation will involve a merge, which you may or may not be\nprepared to resolve.\n\nIf you are used to CVS/SVN workflow where you \"update\" to merge other's\nchanges to your work tree with your own local changes, you will know that\nwith such a merge, depending on the amount of change between G1 and G3,\nyou may end up losing quite a lot of work of your own (G2-G1), when the\nmerge is too complicated for you to handle.  The message you saw is a\nsafety valve to prevent you from trashing your work that way.\n\nThere are two ways to deal with this situation, one safely, and another\nquickly.\n\n - You can \"stash save\" to first save the changes in your work tree,\n   \"checkout\" to switch to branch 'side', and then \"stash pop\" to attempt\n   the merge.\n\n   The last step of unstashing on the new branch _will_ give you the same\n   kind of conflict while computing G3 + (G2 - G1) to update file G, but\n   the approach has one huge advantage compared to CVS/SVN's \"update\", in\n   that you can \"reset --hard\" and \"stash pop\" to reset to a clean state\n   and attempt resolution if you failed resolve conflicts and end up\n   making a mess in your first try.\n\n - If you _know_ that the changes between G1 and G3 do not conflict with\n   what you did between G1 and G2, you can \"checkout -m\" to instruct it to\n   act as if it were CVS/SVN's \"update\" command.\n\n   This can potentially make an unresolvable mess in your work tree, and\n   you can end up losing your changes G2-G1, but it is quicker than \"stash\n   save\"/\"stash pop\" pair.\n"},{"id":"130332","messageId":"vpqpr63cefw.fsf@bauges.imag.fr","threadId":"22024","inReplyTo":"7vhbrghrsn.fsf@alter.siamese.dyndns.org","subject":"Re: You have local changes; cannot switch branches error question","fromName":"Matthieu Moy","fromEmail":"matthieu.moy@grenoble-inp.fr","sentAt":"2009-12-25T14:32:03Z","receivedAt":"2009-12-25T14:32:03Z","isPatch":false,"sender":{"key":"matthieu.moy@grenoble-inp.fr","avatar":"https://gravatar.com/avatar/72c8a2705971a25dfaff23cece15130d405685845d911aedd5667ace277f3fc5?d=mp&s=160"},"body":"Junio C Hamano <gitster@pobox.com> writes:\n\n> There are two ways to deal with this situation, one safely, and another\n> quickly.\n\nAnd a third one : commit, and then rebase onto the other branch. It's\nsafe, and nice when your local changes are ready to commit (then, you\ncan write the commit message before bothering about potential merge\nconflicts).\n\n-- \nMatthieu Moy\nhttp://www-verimag.imag.fr/~moy/\n"}]}