{"thread":{"id":"45897","subject":"[RFC] Add warning when git discard changes on another branch?","startedAt":"2017-05-07T22:35:53Z","lastAt":"2017-05-08T06:11:14Z","messageCount":9,"participants":["Yubin Ruan","Junio C Hamano"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"319020","messageId":"20170507233512.GA9332@HP","threadId":"45897","inReplyTo":null,"subject":"[RFC] Add warning when git discard changes on another branch?","fromName":"Yubin Ruan","fromEmail":"ablacktshirt@gmail.com","sentAt":"2017-05-07T23:35:14Z","receivedAt":"2017-05-07T22:35:53Z","isPatch":false,"sender":{"key":"ablacktshirt@gmail.com","avatar":null},"body":"Hi,\nI think it would be better if git can warn use if we switch to another branch\nwithout committing the modification. Git will warn if the modification is based\non a commit different from where the checkout happened.\n\nFor example, say I am now on branch 'master' and all files *clean*. Now if I do:\n    $ git checkout -b issue\nand make some changes to a file:\n    $ echo \"modification on branch issue\" >> lala.txt\nand then switch back to branch 'master':\n    $ git checkout master\nand git can see the changes:\n    $ git status\n      On branch master\n      Changes not staged for commit:\n        (use \"git add <file>...\" to update what will be committed)\n        (use \"git checkout -- <file>...\" to discard changes in working directory)\n      \n      \tmodified:   lala.txt\n      \n      no changes added to commit (use \"git add\" and/or \"git commit -a\")\n      \nNow, if I do \"git checkout -- lala.txt\", then I will lose that change on branch\n'issue' too!!! \n\nIf, on branch 'issue', the changes to 'lala.txt' are based on a commit different\nfrom where the checkout happened, i.e.\n     \n    on branch 'master'\n        |\n        |  <-- git checkout -b issue\n         \\\n          \\  <-- modification to git happened on a commit different from where\n                 the checkout happened\n\nthen git would warn us something like this:\n\n    error: Your local changes to the following files would be overwritten by checkout:\n    \tlala.txt\n    Please, commit your changes or stash them before you can switch branches.\n    Aborting\n\nSo, I think it is better to add some similar warinings if the modification\nhappened on a commit the *same* as where the checkout happened, i.e.\n\n    on branch 'master'\n        |\n        |  <-- git checkout -b issue\n           <-- modification to git happened on a commit different from where\n               the checkout happened\n\nI already lost some of my work by this ...\n\n(p.s please add me to the Cc because I am not in this list)\n---\nYubin\n"},{"id":"319040","messageId":"xmqqzieo9hfo.fsf@gitster.mtv.corp.google.com","threadId":"45897","inReplyTo":"20170507233512.GA9332@HP","subject":"Re: [RFC] Add warning when git discard changes on another branch?","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2017-05-08T03:05:31Z","receivedAt":"2017-05-08T03:05:42Z","isPatch":false,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Yubin Ruan <ablacktshirt@gmail.com> writes:\n\n> I think it would be better if git can warn use if we switch to another branch\n> without committing the modification. Git will warn if the modification is based\n> on a commit different from where the checkout happened.\n>\n> For example, say I am now on branch 'master' and all files *clean*. Now if I do:\n>     $ git checkout -b issue\n> and make some changes to a file:\n>     $ echo \"modification on branch issue\" >> lala.txt\n> and then switch back to branch 'master':\n>     $ git checkout master\n> and git can see the changes:\n>     $ git status\n>       On branch master\n>       Changes not staged for commit:\n>         (use \"git add <file>...\" to update what will be committed)\n>         (use \"git checkout -- <file>...\" to discard changes in working directory)\n>       \n>       \tmodified:   lala.txt\n>       \n>       no changes added to commit (use \"git add\" and/or \"git commit -a\")\n>       \n> Now, if I do \"git checkout -- lala.txt\", then I will lose that change on branch\n> 'issue' too!!! \n\nThere may be a fundamental misunderstanding here.  In Git, changes\nyou make in the working tree do *not* belong to any branch.  The\nrequest \"git checkout -- lala.txt\" you made in this step does *not*\nsay \"Hey, Git, these changes to lala.txt are not necessary in the\n'master' branch\".  It says \"I started editing lala.txt, but it turns\nout that I do not need that change at all, anywhere, please remove\nit.\"\n\nIf you meant the changes while you were on \"issues\" branch were not\nyet ready to be committed, but now you want to work on \"master\"\nbranch without having to worry about these changes, \"git stash\" may\nbe a useful tool.  Alternatively, you can just create a temporary\ncommit while on \"issues\" branch before checking out \"master\" branch\nto work on something else, and when you are ready to continue\nworking on the \"issues\" branch, check out \"issues\" branch and either\n(1) start with \"reset HEAD^\" or (2) just continue working on it and\nconclude with \"commit --amend\".\n\n"},{"id":"319041","messageId":"20170508111836.GB3014@HP","threadId":"45897","inReplyTo":"xmqqzieo9hfo.fsf@gitster.mtv.corp.google.com","subject":"Re: [RFC] Add warning when git discard changes on another branch?","fromName":"Yubin Ruan","fromEmail":"ablacktshirt@gmail.com","sentAt":"2017-05-08T11:18:39Z","receivedAt":"2017-05-08T03:18:59Z","isPatch":false,"sender":{"key":"ablacktshirt@gmail.com","avatar":null},"body":"On Mon, May 08, 2017 at 12:05:31PM +0900, Junio C Hamano wrote:\n> Yubin Ruan <ablacktshirt@gmail.com> writes:\n> \n> > I think it would be better if git can warn use if we switch to another branch\n> > without committing the modification. Git will warn if the modification is based\n> > on a commit different from where the checkout happened.\n> >\n> > For example, say I am now on branch 'master' and all files *clean*. Now if I do:\n> >     $ git checkout -b issue\n> > and make some changes to a file:\n> >     $ echo \"modification on branch issue\" >> lala.txt\n> > and then switch back to branch 'master':\n> >     $ git checkout master\n> > and git can see the changes:\n> >     $ git status\n> >       On branch master\n> >       Changes not staged for commit:\n> >         (use \"git add <file>...\" to update what will be committed)\n> >         (use \"git checkout -- <file>...\" to discard changes in working directory)\n> >       \n> >       \tmodified:   lala.txt\n> >       \n> >       no changes added to commit (use \"git add\" and/or \"git commit -a\")\n> >       \n> > Now, if I do \"git checkout -- lala.txt\", then I will lose that change on branch\n> > 'issue' too!!! \n> \n> There may be a fundamental misunderstanding here.  In Git, changes\n> you make in the working tree do *not* belong to any branch.  The\n> request \"git checkout -- lala.txt\" you made in this step does *not*\n> say \"Hey, Git, these changes to lala.txt are not necessary in the\n> 'master' branch\".  It says \"I started editing lala.txt, but it turns\n> out that I do not need that change at all, anywhere, please remove\n> it.\"\n\nI understand this. I just suggest that git add some warning in case some users\nare not aware of this, as it does when , on branch 'issue', changes to 'lala.txt'\nare based on a commit different from where the checkout happened, i.e.\n      \n     on branch 'master'\n         |\n         |  <-- git checkout -b issue\n          \\\n           \\  <-- modification to git happened on a commit different from where\n                  the checkout happened\n \nin this situation, git would warn us something like this:\n \n     error: Your local changes to the following files would be overwritten by checkout:\n     \tlala.txt\n     Please, commit your changes or stash them before you can switch branches.\n     Aborting\n\n> If you meant the changes while you were on \"issues\" branch were not\n> yet ready to be committed, but now you want to work on \"master\"\n> branch without having to worry about these changes, \"git stash\" may\n> be a useful tool.  Alternatively, you can just create a temporary\n> commit while on \"issues\" branch before checking out \"master\" branch\n> to work on something else, and when you are ready to continue\n> working on the \"issues\" branch, check out \"issues\" branch and either\n> (1) start with \"reset HEAD^\" or (2) just continue working on it and\n> conclude with \"commit --amend\".\n \nNice suggestion though.\n\n---\nYubin\n"},{"id":"319042","messageId":"xmqqvapc9fsg.fsf@gitster.mtv.corp.google.com","threadId":"45897","inReplyTo":"20170508111836.GB3014@HP","subject":"Re: [RFC] Add warning when git discard changes on another branch?","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2017-05-08T03:41:03Z","receivedAt":"2017-05-08T03:41:15Z","isPatch":false,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Yubin Ruan <ablacktshirt@gmail.com> writes:\n\n> I understand this. I just suggest that git add some warning in case some users\n> are not aware of this, as it does when , on branch 'issue', changes to 'lala.txt'\n> are based on a commit different from where the checkout happened, i.e.\n>       \n>      on branch 'master'\n>          |\n>          |  <-- git checkout -b issue\n>           \\\n>            \\  <-- modification to git happened on a commit different from where\n>                   the checkout happened\n>  \n> in this situation, git would warn us something like this:\n>  \n>      error: Your local changes to the following files would be overwritten by checkout:\n>      \tlala.txt\n>      Please, commit your changes or stash them before you can switch branches.\n>      Aborting\n\nThat does not have much to do with \"are commits the same?\".  If\n'master' and 'issue' branches are pointing at different commit, as\nlong as they record the same content at the path \"lala.txt\", you can\ncheck out between these branches freely, as we can do so without\nhaving to resort to merging the edit the user made to the working\ntree to the different contents of \"lala.txt\".\n\nThere already is an indication that you have local modification in\nthe working tree when we check out another branch (you would have\nseen \"M lala.txt\" when you did a \"checkout\" of another branch while\nyou have local changes to the path).  \n\nBecause it is a quite commonly used feature that you can checkout\nanother branch and carry local changes with you, making it error out\nlike we do when the branches record different contents for the\nlocally changed paths when we do not have to would be a bad change\nthat hurts productivity of users who do use Git correctly, which\nwould mean that we need to make it an opt-in feature. \n\nBut to help \"some users are not aware of this\" situation, an opt-in\n\"feature\" would not help all that much.  The same number of lines in\nthe documentation to tell end-users how to toggle on such a \"safety\"\nfeature can be spent to teach them that their local changes in the\nworking tree do *not* belong to any particular branch, and as soon\nas it is understood, the user would be OK.\n\nSo...\n\n\n"},{"id":"319043","messageId":"20170508120129.GA3540@HP","threadId":"45897","inReplyTo":"xmqqvapc9fsg.fsf@gitster.mtv.corp.google.com","subject":"Re: [RFC] Add warning when git discard changes on another branch?","fromName":"Yubin Ruan","fromEmail":"ablacktshirt@gmail.com","sentAt":"2017-05-08T12:01:32Z","receivedAt":"2017-05-08T04:01:56Z","isPatch":false,"sender":{"key":"ablacktshirt@gmail.com","avatar":null},"body":"On Mon, May 08, 2017 at 12:41:03PM +0900, Junio C Hamano wrote:\n> Yubin Ruan <ablacktshirt@gmail.com> writes:\n> \n> > I understand this. I just suggest that git add some warning in case some users\n> > are not aware of this, as it does when , on branch 'issue', changes to 'lala.txt'\n> > are based on a commit different from where the checkout happened, i.e.\n> >       \n> >      on branch 'master'\n> >          |\n> >          |  <-- git checkout -b issue\n> >           \\\n> >            \\  <-- modification to git happened on a commit different from where\n> >                   the checkout happened\n> >  \n> > in this situation, git would warn us something like this:\n> >  \n> >      error: Your local changes to the following files would be overwritten by checkout:\n> >      \tlala.txt\n> >      Please, commit your changes or stash them before you can switch branches.\n> >      Aborting\n> \n> That does not have much to do with \"are commits the same?\".  If\n> 'master' and 'issue' branches are pointing at different commit, as\n> long as they record the same content at the path \"lala.txt\", you can\n> check out between these branches freely, as we can do so without\n> having to resort to merging the edit the user made to the working\n> tree to the different contents of \"lala.txt\".\n> \n> There already is an indication that you have local modification in\n> the working tree when we check out another branch (you would have\n> seen \"M lala.txt\" when you did a \"checkout\" of another branch while\n> you have local changes to the path).  \n\nThat is convincing :)\n \n> Because it is a quite commonly used feature that you can checkout\n> another branch and carry local changes with you, making it error out\n> like we do when the branches record different contents for the\n> locally changed paths when we do not have to would be a bad change\n> that hurts productivity of users who do use Git correctly, which\n> would mean that we need to make it an opt-in feature. \n\nI agree.\n\n> But to help \"some users are not aware of this\" situation, an opt-in\n> \"feature\" would not help all that much.  The same number of lines in\n> the documentation to tell end-users how to toggle on such a \"safety\"\n> feature can be spent to teach them that their local changes in the\n> working tree do *not* belong to any particular branch, and as soon\n> as it is understood, the user would be OK.\n> \n> So...\n> \n\n---\nYubin\n"},{"id":"319045","messageId":"xmqqlgq89dzl.fsf@gitster.mtv.corp.google.com","threadId":"45897","inReplyTo":"xmqqvapc9fsg.fsf@gitster.mtv.corp.google.com","subject":"Re: [RFC] Add warning when git discard changes on another branch?","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2017-05-08T04:19:58Z","receivedAt":"2017-05-08T04:20:05Z","isPatch":false,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Junio C Hamano <gitster@pobox.com> writes:\n\n> But to help \"some users are not aware of this\" situation, an opt-in\n> \"feature\" would not help all that much.  The same number of lines in\n> the documentation to tell end-users how to toggle on such a \"safety\"\n> feature can be spent to teach them that their local changes in the\n> working tree do *not* belong to any particular branch, and as soon\n> as it is understood, the user would be OK.\n>\n> So...\n\nIt might help if we treat this similarly to how we treat the\n\"detached HEAD\" state.  By default when you do \"git checkout HEAD^0\"\n(not \"git checkout --detach HEAD\"), you would get a large warning,\nwhich you can silence by the advice.detachedhead configuration.  In\naddition to the list of \"these paths have local modifications\" that\nwe show as a reminder, perhaps you want to show a warning that tells\nthe user that the local modifications in these paths are not\nrecorded anywhere else, or somesuch, and silence it with a new\nadvice.* variable?\n"},{"id":"319047","messageId":"20170508122705.GB3540@HP","threadId":"45897","inReplyTo":"xmqqlgq89dzl.fsf@gitster.mtv.corp.google.com","subject":"Re: [RFC] Add warning when git discard changes on another branch?","fromName":"Yubin Ruan","fromEmail":"ablacktshirt@gmail.com","sentAt":"2017-05-08T12:27:08Z","receivedAt":"2017-05-08T04:27:25Z","isPatch":false,"sender":{"key":"ablacktshirt@gmail.com","avatar":null},"body":"On Mon, May 08, 2017 at 01:19:58PM +0900, Junio C Hamano wrote:\n> Junio C Hamano <gitster@pobox.com> writes:\n> \n> > But to help \"some users are not aware of this\" situation, an opt-in\n> > \"feature\" would not help all that much.  The same number of lines in\n> > the documentation to tell end-users how to toggle on such a \"safety\"\n> > feature can be spent to teach them that their local changes in the\n> > working tree do *not* belong to any particular branch, and as soon\n> > as it is understood, the user would be OK.\n> >\n> > So...\n> \n> It might help if we treat this similarly to how we treat the\n> \"detached HEAD\" state.  By default when you do \"git checkout HEAD^0\"\n> (not \"git checkout --detach HEAD\"), you would get a large warning,\n> which you can silence by the advice.detachedhead configuration.  In\n> addition to the list of \"these paths have local modifications\" that\n> we show as a reminder, perhaps you want to show a warning that tells\n> the user that the local modifications in these paths are not\n> recorded anywhere else, or somesuch, and silence it with a new\n> advice.* variable?\n\nThat would be helpful. But, frankly, if a user would be aware of that `advice.*'\nvariable, he would have enough knowledge of Git to be aware of that situation.\nSo, I think that 'M lala.txt' in transitions from branch to branch would be\nsufficient.\n\n---\nYubin\n"},{"id":"319058","messageId":"xmqq60hb9915.fsf@gitster.mtv.corp.google.com","threadId":"45897","inReplyTo":"20170508122705.GB3540@HP","subject":"Re: [RFC] Add warning when git discard changes on another branch?","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2017-05-08T06:07:02Z","receivedAt":"2017-05-08T06:07:09Z","isPatch":false,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Yubin Ruan <ablacktshirt@gmail.com> writes:\n\n> That would be helpful. But, frankly, if a user would be aware of that `advice.*'\n> variable, he would have enough knowledge of Git to be aware of that situation.\n> So, I think that 'M lala.txt' in transitions from branch to branch would be\n> sufficient.\n\nAh, you got the advice.* thing wrong.  The \"advice\" messages are by\ndefault noisy, and experts can turn them down by setting advice.* to\n\"false\", saying \"I know Git well enough that I do not need handholding\nin this area.\"\n\nSo a newbie does not have to know about \"advice.*\".  When s/he gets\nexperienced enough to get annoyed by noisy messages, s/he can tune\nit out.\n"},{"id":"319059","messageId":"20170508141048.GA3463@HP","threadId":"45897","inReplyTo":"xmqq60hb9915.fsf@gitster.mtv.corp.google.com","subject":"Re: [RFC] Add warning when git discard changes on another branch?","fromName":"Yubin Ruan","fromEmail":"ablacktshirt@gmail.com","sentAt":"2017-05-08T14:10:51Z","receivedAt":"2017-05-08T06:11:14Z","isPatch":false,"sender":{"key":"ablacktshirt@gmail.com","avatar":null},"body":"On Mon, May 08, 2017 at 03:07:02PM +0900, Junio C Hamano wrote:\n> Yubin Ruan <ablacktshirt@gmail.com> writes:\n> \n> > That would be helpful. But, frankly, if a user would be aware of that `advice.*'\n> > variable, he would have enough knowledge of Git to be aware of that situation.\n> > So, I think that 'M lala.txt' in transitions from branch to branch would be\n> > sufficient.\n> \n> Ah, you got the advice.* thing wrong.  The \"advice\" messages are by\n> default noisy, and experts can turn them down by setting advice.* to\n> \"false\", saying \"I know Git well enough that I do not need handholding\n> in this area.\"\n> \n> So a newbie does not have to know about \"advice.*\".  When s/he gets\n> experienced enough to get annoyed by noisy messages, s/he can tune\n> it out.\n\nYou are right. I might have had too much drug right then...\nBut still, I don't think that advice.* is necessary.\n\n---\nYubin\n"}]}