{"thread":{"id":"47214","subject":"[Feature- Request] Option to commit after checking out branch command is made","startedAt":"2017-11-15T15:34:50Z","lastAt":"2019-06-03T08:44:55Z","messageCount":4,"participants":["Ninivaggi Mattia","Junio C Hamano","David Eisner"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"332606","messageId":"670f44c0ac554f2faec5a6112376b015@MSSMAILP06.ads.hel.kko.ch","threadId":"47214","inReplyTo":null,"subject":"[Feature- Request] Option to commit after checking out branch command is made","fromName":"Ninivaggi Mattia","fromEmail":"mattia.ninivaggi@helsana.ch","sentAt":"2017-11-15T15:27:18Z","receivedAt":"2017-11-15T15:34:50Z","isPatch":false,"sender":{"key":"mattia.ninivaggi@helsana.ch","avatar":null},"body":"Hey guys\n\nSometimes I tend to forget to commit changes before I checkout another branch and the following scenario happens (via cli on windows [with git bash]):\n\n1. I checkout a branch, without having commited first\n    > git checkout dev\n2. I get this error message:\n    > error: Your local changes to the following files would be overwritten by checkout:\n    > // List of files\n    > // ..\n    > //\n    > Please commit your changes or stash them before you switch branches.\n\nBut I would rather prefer a scenario like this:\n\n1. I checkout a branch, without having commited first\n    > git checkout dev\n2. I get a message like this:\n    > Your local changes to the following files would be overwritten by checkout:\n    > // List of files\n    > // ..\n    > //\n    > Would you want to commit first? (y/n))\n\nIF y --> prompt for commit message and commit automatically\nIF n --> display error message: \"Please commit your changes or stash them before you switch branches\"\n\nThis would be a faster/ more productive way to handle this error.\n\nI think it wont be a big challenge, you just should put a message in the cli-tool when this error occurs and call the commit functionality if \"y\" is pressed. If you'd like I'll even consider doing it myself. If you have some documentation or some tipps on where to look for it.\n\nCheers\n\nNinivaggi Mattia\n"},{"id":"332628","messageId":"xmqqtvxvxefx.fsf@gitster.mtv.corp.google.com","threadId":"47214","inReplyTo":"670f44c0ac554f2faec5a6112376b015@MSSMAILP06.ads.hel.kko.ch","subject":"Re: [Feature- Request] Option to commit after checking out branch command is made","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2017-11-16T00:19:46Z","receivedAt":"2017-11-16T00:19:56Z","isPatch":false,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Ninivaggi Mattia <mattia.ninivaggi@helsana.ch> writes:\n\n> 1. I checkout a branch, without having commited first\n>     > git checkout dev\n> 2. I get this error message:\n>     > error: Your local changes to the following files would be overwritten by checkout:\n>     > // List of files\n>     > // ..\n>     > //\n>     > Please commit your changes or stash them before you switch branches.\n>\n> But I would rather prefer a scenario like this:\n> ...\n> 1. I checkout a branch, without having commited first\n>     > git checkout dev\n> 2. I get a message like this:\n>     > Your local changes to the following files would be overwritten by checkout:\n>     > // List of files\n>     > // ..\n>     > //\n>     > Would you want to commit first? (y/n))\n>\n> IF y --> prompt for commit message and commit automatically\n\nI do not think you want to do this for a few reasons.\n\n * The \"please commit or stash\" is merely a suggestion whose primary\n   purpose is to silence clueless newbies who would have complained\n   \"Git said 'error: ... overwritten by checkout' and I do not know\n   what to do next; the error message is so unhelpful\" otherwise.\n   Majority of the time when I see this message, it is because I\n   forgot that I was in the middle of doing something (meaning: I am\n   far from finished with the changes I was working on), and I would\n   not be ready to commit.  I'd rather keep working to get the work\n   into a more reasonable shape before committing, or stash the\n   changes first if the task I wanted to do on that \"dev\" branch is\n   more urgent and what I was in the middle of doing is lower\n   priority.  \n\n   Because of this, I would expect many users (including the ones\n   who are right now newbies but will have gained experience to\n   become experts in the future) to appreciate \"stash before switch\"\n   a lot more than \"commit first before switch\".\n\n * People write scripts that use \"git checkout\" to switch branches,\n   and they rely on the command to fail in this situation, instead\n   of going interactive and gets stuck waiting for an input (which\n   may never come).  Because of this, the updated behaviour you\n   propose must never be the default, and at least must be protected\n   behind a flag, something like \"git checkout --autostash dev\" (or\n   \"--autocommit\", if you insist).  With that, you could do\n\n\t[alias]\n\t\tco = checkout --autostash\n\n   and train your fingers to say \"git co dev\".\n\nOf course, you can have a \"git-co\" script on your $PATH, which runs\n\"git checkout $1\", lets it fail just like it does now, and then does\n\"git commit\", if you really want the behaviour.  Again, you can then\nuse \"git co dev\" and you do not have to worry about breaking\npeople's scripts that depends on \"git checkout\" to fail without\ngoing interactive.\n"},{"id":"332661","messageId":"b97ebd6b7edd4c17a9c87f22af13ee6c@MSSMAILP06.ads.hel.kko.ch","threadId":"47214","inReplyTo":"xmqqtvxvxefx.fsf@gitster.mtv.corp.google.com","subject":"RE: [Feature- Request] Option to commit after checking out branch command is made","fromName":"Ninivaggi Mattia","fromEmail":"mattia.ninivaggi@helsana.ch","sentAt":"2017-11-16T11:19:19Z","receivedAt":"2017-11-16T11:19:56Z","isPatch":false,"sender":{"key":"mattia.ninivaggi@helsana.ch","avatar":null},"body":"I like the --autostash flag. \nBut actually you just could write an alias co=\"git stash && git checkout\" and use co dev for the same purpose, rendering the change irrelevant.\n\n-----Original Message-----\nFrom: Junio C Hamano [mailto:gitster@pobox.com] \nSent: Thursday, November 16, 2017 1:20 AM\nTo: Ninivaggi Mattia\nCc: git@vger.kernel.org\nSubject: Re: [Feature- Request] Option to commit after checking out branch command is made\n\nNinivaggi Mattia <mattia.ninivaggi@helsana.ch> writes:\n\n> 1. I checkout a branch, without having commited first\n>     > git checkout dev\n> 2. I get this error message:\n>     > error: Your local changes to the following files would be overwritten by checkout:\n>     > // List of files\n>     > // ..\n>     > //\n>     > Please commit your changes or stash them before you switch branches.\n>\n> But I would rather prefer a scenario like this:\n> ...\n> 1. I checkout a branch, without having commited first\n>     > git checkout dev\n> 2. I get a message like this:\n>     > Your local changes to the following files would be overwritten by checkout:\n>     > // List of files\n>     > // ..\n>     > //\n>     > Would you want to commit first? (y/n))\n>\n> IF y --> prompt for commit message and commit automatically\n\nI do not think you want to do this for a few reasons.\n\n * The \"please commit or stash\" is merely a suggestion whose primary\n   purpose is to silence clueless newbies who would have complained\n   \"Git said 'error: ... overwritten by checkout' and I do not know\n   what to do next; the error message is so unhelpful\" otherwise.\n   Majority of the time when I see this message, it is because I\n   forgot that I was in the middle of doing something (meaning: I am\n   far from finished with the changes I was working on), and I would\n   not be ready to commit.  I'd rather keep working to get the work\n   into a more reasonable shape before committing, or stash the\n   changes first if the task I wanted to do on that \"dev\" branch is\n   more urgent and what I was in the middle of doing is lower\n   priority.  \n\n   Because of this, I would expect many users (including the ones\n   who are right now newbies but will have gained experience to\n   become experts in the future) to appreciate \"stash before switch\"\n   a lot more than \"commit first before switch\".\n\n * People write scripts that use \"git checkout\" to switch branches,\n   and they rely on the command to fail in this situation, instead\n   of going interactive and gets stuck waiting for an input (which\n   may never come).  Because of this, the updated behaviour you\n   propose must never be the default, and at least must be protected\n   behind a flag, something like \"git checkout --autostash dev\" (or\n   \"--autocommit\", if you insist).  With that, you could do\n\n\t[alias]\n\t\tco = checkout --autostash\n\n   and train your fingers to say \"git co dev\".\n\nOf course, you can have a \"git-co\" script on your $PATH, which runs \"git checkout $1\", lets it fail just like it does now, and then does \"git commit\", if you really want the behaviour.  Again, you can then use \"git co dev\" and you do not have to worry about breaking people's scripts that depends on \"git checkout\" to fail without going interactive.\n"},{"id":"376557","messageId":"CAGe7hXCqC9hYc3brekqcT4A5O4muzAL=7RzHjviSusMMY5qc_Q@mail.gmail.com","threadId":"47214","inReplyTo":"CAGe7hXBPWvjaKZtz-Zn1az0HrCx=OpxGsghVJhLOBKMu3NJ2zA@mail.gmail.com","subject":"Fwd: [Feature- Request] Option to commit after checking out branch command is made","fromName":"David Eisner","fromEmail":"david.eisner@oriel.oxon.org","sentAt":"2019-06-03T08:44:39Z","receivedAt":"2019-06-03T08:44:55Z","isPatch":false,"sender":{"key":"david.eisner@oriel.oxon.org","avatar":null},"body":"Hi all\n\nInteresting discussion.\n\nThough it's a pretty distant memory now, this \"forgot to commit\"\nscenario was really frequent for me when I started using git. Then I'd\nrun commit and forget that it's split (from an outsider's perspective)\ninto add and then commit. I like the design of git, trees, index, etc.\nnow that I get it, but being kicked from one command to another with\nan error message which may or may not tell me which command I should\nhave used is not fun.\n\nSurely if it's an interactive \"commit/stash/cancel\" prompt, it could\ndefault to cancel for non-interactive settings. If you're running a\nscript interactively that just does \"git commit\" and ignores the\noutput, the prompt would be fine. If you're running a script in a\nnon-interactive way, piping the command within the script, even just\nto logs, etc. the prompt will not appear, just like with log or diff,\nwhich do not use a pager in non-interactive contexts.\n\nGenerally, I think that prompting by default instead of warning and\nquitting is a really good way to go. It more gently initiates the new\nuser, while still giving the option to quit and deal with the issue\nanother way. Seasoned users should be able to disable individual\nprompts or all using options. Scripting should be a secondary\nconsideration for porcelain, because you can build more reliable\nscripts by either using porcelain or being more explicit with\narguments and options. For example, in a script, it would be better to\nspecify the remote and branches for a push, rather than using\ndefaults.\n\nAutostash/autocommit options would be great, if they also have \"git\nconfig checkout.autostash/autocommit\" and --no-autostash/autocommit\nand are set to prompt by default, or perhaps one autostash option that\ncan be set to false/stash/commit/prompt.\n\nYes, you could do this with scripting over the top of the existing\ncommands, but the best experience for new users should be available\nout of the box, not by writing scripts.\n\nOn Thu, 16 Nov 2017 at 01:07, Junio C Hamano <gitster@pobox.com> wrote:\n>\n> Ninivaggi Mattia <mattia.ninivaggi@helsana.ch> writes:\n>\n> > 1. I checkout a branch, without having commited first\n> >     > git checkout dev\n> > 2. I get this error message:\n> >     > error: Your local changes to the following files would be overwritten by checkout:\n> >     > // List of files\n> >     > // ..\n> >     > //\n> >     > Please commit your changes or stash them before you switch branches.\n> >\n> > But I would rather prefer a scenario like this:\n> > ...\n> > 1. I checkout a branch, without having commited first\n> >     > git checkout dev\n> > 2. I get a message like this:\n> >     > Your local changes to the following files would be overwritten by checkout:\n> >     > // List of files\n> >     > // ..\n> >     > //\n> >     > Would you want to commit first? (y/n))\n> >\n> > IF y --> prompt for commit message and commit automatically\n>\n> I do not think you want to do this for a few reasons.\n>\n>  * The \"please commit or stash\" is merely a suggestion whose primary\n>    purpose is to silence clueless newbies who would have complained\n>    \"Git said 'error: ... overwritten by checkout' and I do not know\n>    what to do next; the error message is so unhelpful\" otherwise.\n>    Majority of the time when I see this message, it is because I\n>    forgot that I was in the middle of doing something (meaning: I am\n>    far from finished with the changes I was working on), and I would\n>    not be ready to commit.  I'd rather keep working to get the work\n>    into a more reasonable shape before committing, or stash the\n>    changes first if the task I wanted to do on that \"dev\" branch is\n>    more urgent and what I was in the middle of doing is lower\n>    priority.\n>\n>    Because of this, I would expect many users (including the ones\n>    who are right now newbies but will have gained experience to\n>    become experts in the future) to appreciate \"stash before switch\"\n>    a lot more than \"commit first before switch\".\n>\n>  * People write scripts that use \"git checkout\" to switch branches,\n>    and they rely on the command to fail in this situation, instead\n>    of going interactive and gets stuck waiting for an input (which\n>    may never come).  Because of this, the updated behaviour you\n>    propose must never be the default, and at least must be protected\n>    behind a flag, something like \"git checkout --autostash dev\" (or\n>    \"--autocommit\", if you insist).  With that, you could do\n>\n>         [alias]\n>                 co = checkout --autostash\n>\n>    and train your fingers to say \"git co dev\".\n>\n> Of course, you can have a \"git-co\" script on your $PATH, which runs\n> \"git checkout $1\", lets it fail just like it does now, and then does\n> \"git commit\", if you really want the behaviour.  Again, you can then\n> use \"git co dev\" and you do not have to worry about breaking\n> people's scripts that depends on \"git checkout\" to fail without\n> going interactive.\n"}]}