{"thread":{"id":"30798","subject":"Prevent switching branches when local modifications exist","startedAt":"2012-06-13T22:52:01Z","lastAt":"2012-06-14T07:17:30Z","messageCount":3,"participants":["Christoph Bartoschek","Junio C Hamano"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"193603","messageId":"h5ina9-g9n.ln1@homer.bruehl.pontohonk.de","threadId":"30798","inReplyTo":null,"subject":"Prevent switching branches when local modifications exist","fromName":"Christoph Bartoschek","fromEmail":"ponto@pontohonk.de","sentAt":"2012-06-13T22:52:01Z","receivedAt":"2012-06-13T22:52:01Z","isPatch":false,"sender":{"key":"ponto@pontohonk.de","avatar":null},"body":"Hi,\n\nhow can I prevent that git allows me to switch branches when there are \nuncommited local modifications?\n\n[14.1 (steiner_topo)]$ git checkout -b ergo\nSwitched to a new branch 'ergo'\n[14.1 (ergo)]$ echo blub >> src/invtree.C\n[14.1 (ergo)]$ git checkout steiner_topo\nM       src/invtree.C\nSwitched to branch 'steiner_topo'\n[14.1 (steiner_topo)]$ \n\nHow can the last checkout be prevented?\n\n-- \nChristoph Bartoschek\n"},{"id":"193605","messageId":"7vfw9yg4z1.fsf@alter.siamese.dyndns.org","threadId":"30798","inReplyTo":"h5ina9-g9n.ln1@homer.bruehl.pontohonk.de","subject":"Re: Prevent switching branches when local modifications exist","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2012-06-13T23:35:46Z","receivedAt":"2012-06-13T23:35:46Z","isPatch":false,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Christoph Bartoschek <ponto@pontohonk.de> writes:\n\n> Hi,\n>\n> how can I prevent that git allows me to switch branches when there are \n> uncommited local modifications?\n>\n> [14.1 (steiner_topo)]$ git checkout -b ergo\n> Switched to a new branch 'ergo'\n> [14.1 (ergo)]$ echo blub >> src/invtree.C\n> [14.1 (ergo)]$ git checkout steiner_topo\n> M       src/invtree.C\n> Switched to branch 'steiner_topo'\n> [14.1 (steiner_topo)]$ \n>\n> How can the last checkout be prevented?\n\nThe question sounds similar to asking \"How can I prevent 'rm -r'\nfrom removing anything when I type it in my home directory.\"  It is\nuseful for 'rm -r' to remove everything recursively, but sometimes\nyou would want to prevent you from running it by mistake in a wrong\nplace. In general, there is no good direct solution for that.\n\nA few usual ways people deal with this kind of issue are (1) to make\nit easier to notice that they are \"in a wrong place\" (e.g. by having\nthe current directory in their prompt to avoid 'rm -r' in $HOME) and\ntraining themselves to be careful, (2) to make it possible to\nrecover if that happens by mistake (e.g. by having .snapshot on the\nfiler).\n\nFor \"checkout\", an approach that corresponds to (1) is to have\nbranches and status in the prompt (available from git-completion).\nFortunately for (2) there isn't anything special necessary, as\nchecking out a different branch with \"git checkout\" does not lose\ninformation, after creating and checking out the steiner_topo\nbranch, you can use checkout again to come back to ergo branch.\n"},{"id":"193620","messageId":"apfoa9-qkr.ln1@homer.bruehl.pontohonk.de","threadId":"30798","inReplyTo":"7vfw9yg4z1.fsf@alter.siamese.dyndns.org","subject":"Re: Prevent switching branches when local modifications exist","fromName":"Christoph Bartoschek","fromEmail":"ponto@pontohonk.de","sentAt":"2012-06-14T07:17:30Z","receivedAt":"2012-06-14T07:17:30Z","isPatch":false,"sender":{"key":"ponto@pontohonk.de","avatar":null},"body":"Junio C Hamano wrote:\n\n> Christoph Bartoschek <ponto@pontohonk.de> writes:\n> \n>> Hi,\n>>\n>> how can I prevent that git allows me to switch branches when there are\n>> uncommited local modifications?\n>>\n>> [14.1 (steiner_topo)]$ git checkout -b ergo\n>> Switched to a new branch 'ergo'\n>> [14.1 (ergo)]$ echo blub >> src/invtree.C\n>> [14.1 (ergo)]$ git checkout steiner_topo\n>> M       src/invtree.C\n>> Switched to branch 'steiner_topo'\n>> [14.1 (steiner_topo)]$\n>>\n>> How can the last checkout be prevented?\n> \n> The question sounds similar to asking \"How can I prevent 'rm -r'\n> from removing anything when I type it in my home directory.\"  It is\n> useful for 'rm -r' to remove everything recursively, but sometimes\n> you would want to prevent you from running it by mistake in a wrong\n> place. In general, there is no good direct solution for that.\n> \n> A few usual ways people deal with this kind of issue are (1) to make\n> it easier to notice that they are \"in a wrong place\" (e.g. by having\n> the current directory in their prompt to avoid 'rm -r' in $HOME) and\n> training themselves to be careful, (2) to make it possible to\n> recover if that happens by mistake (e.g. by having .snapshot on the\n> filer).\n> \n> For \"checkout\", an approach that corresponds to (1) is to have\n> branches and status in the prompt (available from git-completion).\n> Fortunately for (2) there isn't anything special necessary, as\n> checking out a different branch with \"git checkout\" does not lose\n> information, after creating and checking out the steiner_topo\n> branch, you can use checkout again to come back to ergo branch.\n\n\nSo I assume there is no option for this currently.\n\nGit checkout already behaves different from 'rm -r' by preventing me from \noverwritting modified files with changes in the target branch. I just would \nlike to expand this to all files. A branch change is only possible if \neverything is clean. (I see that this does not carry over to untracked \nfiles).\n\nMaybe I have to go with a wrapper for checkout like:\n\nif [[ $(git status --porcelain) =~ \"M \" ]]; then echo \"NO\"; exit 0; else git \ncheckout \"$@\"; fi\n\nA little background: We currently evaluate adoption of git instead of svn \nand some users are scared of any code change by a tool. They even do not \ntrust any merge at all. Before they run 'svn update' they copy all their \nchanges away, carefully merge manually and then go on. \n\nThanks\nChristoph\n"}]}