git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [RFC] Add warning when git discard changes on another branch?

From
Junio C Hamano <gitster@pobox.com>
Date
May 8, 2017, 03:41 UTC
Message-ID
<xmqqvapc9fsg.fsf@gitster.mtv.corp.google.com>
In-Reply-To
<20170508111836.GB3014@HP>
Yubin Ruan <ablacktshirt@gmail.com> writes:
Show 17 quoted lines
> I understand this. I just suggest that git add some warning in case some users
> are not aware of this, as it does when , on branch 'issue', changes to 'lala.txt'
> are based on a commit different from where the checkout happened, i.e.
>       
>      on branch 'master'
>          |
>          |  <-- git checkout -b issue
>           \
>            \  <-- modification to git happened on a commit different from where
>                   the checkout happened
>  
> in this situation, git would warn us something like this:
>  
>      error: Your local changes to the following files would be overwritten by checkout:
>      	lala.txt
>      Please, commit your changes or stash them before you can switch branches.
>      Aborting

That does not have much to do with "are commits the same?". If 'master' and 'issue' branches are pointing at different commit, as long as they record the same content at the path "lala.txt", you can check out between these branches freely, as we can do so without having to resort to merging the edit the user made to the working tree to the different contents of "lala.txt".

There already is an indication that you have local modification in the working tree when we check out another branch (you would have seen "M lala.txt" when you did a "checkout" of another branch while you have local changes to the path).

Because it is a quite commonly used feature that you can checkout another branch and carry local changes with you, making it error out like we do when the branches record different contents for the locally changed paths when we do not have to would be a bad change that hurts productivity of users who do use Git correctly, which would mean that we need to make it an opt-in feature.

But to help "some users are not aware of this" situation, an opt-in "feature" would not help all that much. The same number of lines in the documentation to tell end-users how to toggle on such a "safety" feature can be spent to teach them that their local changes in the working tree do *not* belong to any particular branch, and as soon as it is understood, the user would be OK.

So...
Previous: Yubin RuanNext: Yubin Ruan
Message 4 of 9 in “[RFC] Add warning when git discard changes on another branch?”
  1. Yubin RuanMay 7, 2017
  2. Junio C HamanoMay 8, 2017
  3. Yubin RuanMay 8, 2017
  4. Junio C HamanoMay 8, 2017
  5. Yubin RuanMay 8, 2017
  6. Junio C HamanoMay 8, 2017
  7. Yubin RuanMay 8, 2017
  8. Junio C HamanoMay 8, 2017
  9. Yubin RuanMay 8, 2017

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.