git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* [RFC] Add warning when git discard changes on another branch?
@ 2017-05-07 23:35 Yubin Ruan
  2017-05-08  3:05 ` Junio C Hamano
  0 siblings, 1 reply; 9+ messages in thread
From: Yubin Ruan @ 2017-05-07 23:35 UTC (permalink / raw)
  To: git

Hi,
I think it would be better if git can warn use if we switch to another branch
without committing the modification. Git will warn if the modification is based
on a commit different from where the checkout happened.

For example, say I am now on branch 'master' and all files *clean*. Now if I do:
    $ git checkout -b issue
and make some changes to a file:
    $ echo "modification on branch issue" >> lala.txt
and then switch back to branch 'master':
    $ git checkout master
and git can see the changes:
    $ git status
      On branch master
      Changes not staged for commit:
        (use "git add <file>..." to update what will be committed)
        (use "git checkout -- <file>..." to discard changes in working directory)
      
      	modified:   lala.txt
      
      no changes added to commit (use "git add" and/or "git commit -a")
      
Now, if I do "git checkout -- lala.txt", then I will lose that change on branch
'issue' too!!! 

If, on branch 'issue', the 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

then 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

So, I think it is better to add some similar warinings if the modification
happened on a commit the *same* as 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

I already lost some of my work by this ...

(p.s please add me to the Cc because I am not in this list)
---
Yubin

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [RFC] Add warning when git discard changes on another branch?
  2017-05-07 23:35 [RFC] Add warning when git discard changes on another branch? Yubin Ruan
@ 2017-05-08  3:05 ` Junio C Hamano
  2017-05-08 11:18   ` Yubin Ruan
  0 siblings, 1 reply; 9+ messages in thread
From: Junio C Hamano @ 2017-05-08  3:05 UTC (permalink / raw)
  To: Yubin Ruan; +Cc: git

Yubin Ruan <ablacktshirt@gmail.com> writes:

> I think it would be better if git can warn use if we switch to another branch
> without committing the modification. Git will warn if the modification is based
> on a commit different from where the checkout happened.
>
> For example, say I am now on branch 'master' and all files *clean*. Now if I do:
>     $ git checkout -b issue
> and make some changes to a file:
>     $ echo "modification on branch issue" >> lala.txt
> and then switch back to branch 'master':
>     $ git checkout master
> and git can see the changes:
>     $ git status
>       On branch master
>       Changes not staged for commit:
>         (use "git add <file>..." to update what will be committed)
>         (use "git checkout -- <file>..." to discard changes in working directory)
>       
>       	modified:   lala.txt
>       
>       no changes added to commit (use "git add" and/or "git commit -a")
>       
> Now, if I do "git checkout -- lala.txt", then I will lose that change on branch
> 'issue' too!!! 

There may be a fundamental misunderstanding here.  In Git, changes
you make in the working tree do *not* belong to any branch.  The
request "git checkout -- lala.txt" you made in this step does *not*
say "Hey, Git, these changes to lala.txt are not necessary in the
'master' branch".  It says "I started editing lala.txt, but it turns
out that I do not need that change at all, anywhere, please remove
it."

If you meant the changes while you were on "issues" branch were not
yet ready to be committed, but now you want to work on "master"
branch without having to worry about these changes, "git stash" may
be a useful tool.  Alternatively, you can just create a temporary
commit while on "issues" branch before checking out "master" branch
to work on something else, and when you are ready to continue
working on the "issues" branch, check out "issues" branch and either
(1) start with "reset HEAD^" or (2) just continue working on it and
conclude with "commit --amend".


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [RFC] Add warning when git discard changes on another branch?
  2017-05-08 11:18   ` Yubin Ruan
@ 2017-05-08  3:41     ` Junio C Hamano
  2017-05-08  4:19       ` Junio C Hamano
  2017-05-08 12:01       ` Yubin Ruan
  0 siblings, 2 replies; 9+ messages in thread
From: Junio C Hamano @ 2017-05-08  3:41 UTC (permalink / raw)
  To: Yubin Ruan; +Cc: git

Yubin Ruan <ablacktshirt@gmail.com> writes:

> 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...



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [RFC] Add warning when git discard changes on another branch?
  2017-05-08  3:41     ` Junio C Hamano
@ 2017-05-08  4:19       ` Junio C Hamano
  2017-05-08 12:27         ` Yubin Ruan
  2017-05-08 12:01       ` Yubin Ruan
  1 sibling, 1 reply; 9+ messages in thread
From: Junio C Hamano @ 2017-05-08  4:19 UTC (permalink / raw)
  To: Yubin Ruan; +Cc: git

Junio C Hamano <gitster@pobox.com> writes:

> 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...

It might help if we treat this similarly to how we treat the
"detached HEAD" state.  By default when you do "git checkout HEAD^0"
(not "git checkout --detach HEAD"), you would get a large warning,
which you can silence by the advice.detachedhead configuration.  In
addition to the list of "these paths have local modifications" that
we show as a reminder, perhaps you want to show a warning that tells
the user that the local modifications in these paths are not
recorded anywhere else, or somesuch, and silence it with a new
advice.* variable?

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [RFC] Add warning when git discard changes on another branch?
  2017-05-08 12:27         ` Yubin Ruan
@ 2017-05-08  6:07           ` Junio C Hamano
  2017-05-08 14:10             ` Yubin Ruan
  0 siblings, 1 reply; 9+ messages in thread
From: Junio C Hamano @ 2017-05-08  6:07 UTC (permalink / raw)
  To: Yubin Ruan; +Cc: git

Yubin Ruan <ablacktshirt@gmail.com> writes:

> That would be helpful. But, frankly, if a user would be aware of that `advice.*'
> variable, he would have enough knowledge of Git to be aware of that situation.
> So, I think that 'M lala.txt' in transitions from branch to branch would be
> sufficient.

Ah, you got the advice.* thing wrong.  The "advice" messages are by
default noisy, and experts can turn them down by setting advice.* to
"false", saying "I know Git well enough that I do not need handholding
in this area."

So a newbie does not have to know about "advice.*".  When s/he gets
experienced enough to get annoyed by noisy messages, s/he can tune
it out.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [RFC] Add warning when git discard changes on another branch?
  2017-05-08  3:05 ` Junio C Hamano
@ 2017-05-08 11:18   ` Yubin Ruan
  2017-05-08  3:41     ` Junio C Hamano
  0 siblings, 1 reply; 9+ messages in thread
From: Yubin Ruan @ 2017-05-08 11:18 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git

On Mon, May 08, 2017 at 12:05:31PM +0900, Junio C Hamano wrote:
> Yubin Ruan <ablacktshirt@gmail.com> writes:
> 
> > I think it would be better if git can warn use if we switch to another branch
> > without committing the modification. Git will warn if the modification is based
> > on a commit different from where the checkout happened.
> >
> > For example, say I am now on branch 'master' and all files *clean*. Now if I do:
> >     $ git checkout -b issue
> > and make some changes to a file:
> >     $ echo "modification on branch issue" >> lala.txt
> > and then switch back to branch 'master':
> >     $ git checkout master
> > and git can see the changes:
> >     $ git status
> >       On branch master
> >       Changes not staged for commit:
> >         (use "git add <file>..." to update what will be committed)
> >         (use "git checkout -- <file>..." to discard changes in working directory)
> >       
> >       	modified:   lala.txt
> >       
> >       no changes added to commit (use "git add" and/or "git commit -a")
> >       
> > Now, if I do "git checkout -- lala.txt", then I will lose that change on branch
> > 'issue' too!!! 
> 
> There may be a fundamental misunderstanding here.  In Git, changes
> you make in the working tree do *not* belong to any branch.  The
> request "git checkout -- lala.txt" you made in this step does *not*
> say "Hey, Git, these changes to lala.txt are not necessary in the
> 'master' branch".  It says "I started editing lala.txt, but it turns
> out that I do not need that change at all, anywhere, please remove
> it."

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

> If you meant the changes while you were on "issues" branch were not
> yet ready to be committed, but now you want to work on "master"
> branch without having to worry about these changes, "git stash" may
> be a useful tool.  Alternatively, you can just create a temporary
> commit while on "issues" branch before checking out "master" branch
> to work on something else, and when you are ready to continue
> working on the "issues" branch, check out "issues" branch and either
> (1) start with "reset HEAD^" or (2) just continue working on it and
> conclude with "commit --amend".
 
Nice suggestion though.

---
Yubin

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [RFC] Add warning when git discard changes on another branch?
  2017-05-08  3:41     ` Junio C Hamano
  2017-05-08  4:19       ` Junio C Hamano
@ 2017-05-08 12:01       ` Yubin Ruan
  1 sibling, 0 replies; 9+ messages in thread
From: Yubin Ruan @ 2017-05-08 12:01 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git

On Mon, May 08, 2017 at 12:41:03PM +0900, Junio C Hamano wrote:
> Yubin Ruan <ablacktshirt@gmail.com> writes:
> 
> > 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).  

That is convincing :)
 
> 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. 

I agree.

> 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...
> 

---
Yubin

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [RFC] Add warning when git discard changes on another branch?
  2017-05-08  4:19       ` Junio C Hamano
@ 2017-05-08 12:27         ` Yubin Ruan
  2017-05-08  6:07           ` Junio C Hamano
  0 siblings, 1 reply; 9+ messages in thread
From: Yubin Ruan @ 2017-05-08 12:27 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git

On Mon, May 08, 2017 at 01:19:58PM +0900, Junio C Hamano wrote:
> Junio C Hamano <gitster@pobox.com> writes:
> 
> > 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...
> 
> It might help if we treat this similarly to how we treat the
> "detached HEAD" state.  By default when you do "git checkout HEAD^0"
> (not "git checkout --detach HEAD"), you would get a large warning,
> which you can silence by the advice.detachedhead configuration.  In
> addition to the list of "these paths have local modifications" that
> we show as a reminder, perhaps you want to show a warning that tells
> the user that the local modifications in these paths are not
> recorded anywhere else, or somesuch, and silence it with a new
> advice.* variable?

That would be helpful. But, frankly, if a user would be aware of that `advice.*'
variable, he would have enough knowledge of Git to be aware of that situation.
So, I think that 'M lala.txt' in transitions from branch to branch would be
sufficient.

---
Yubin

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [RFC] Add warning when git discard changes on another branch?
  2017-05-08  6:07           ` Junio C Hamano
@ 2017-05-08 14:10             ` Yubin Ruan
  0 siblings, 0 replies; 9+ messages in thread
From: Yubin Ruan @ 2017-05-08 14:10 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git

On Mon, May 08, 2017 at 03:07:02PM +0900, Junio C Hamano wrote:
> Yubin Ruan <ablacktshirt@gmail.com> writes:
> 
> > That would be helpful. But, frankly, if a user would be aware of that `advice.*'
> > variable, he would have enough knowledge of Git to be aware of that situation.
> > So, I think that 'M lala.txt' in transitions from branch to branch would be
> > sufficient.
> 
> Ah, you got the advice.* thing wrong.  The "advice" messages are by
> default noisy, and experts can turn them down by setting advice.* to
> "false", saying "I know Git well enough that I do not need handholding
> in this area."
> 
> So a newbie does not have to know about "advice.*".  When s/he gets
> experienced enough to get annoyed by noisy messages, s/he can tune
> it out.

You are right. I might have had too much drug right then...
But still, I don't think that advice.* is necessary.

---
Yubin

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2017-05-08  6:11 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-05-07 23:35 [RFC] Add warning when git discard changes on another branch? Yubin Ruan
2017-05-08  3:05 ` Junio C Hamano
2017-05-08 11:18   ` Yubin Ruan
2017-05-08  3:41     ` Junio C Hamano
2017-05-08  4:19       ` Junio C Hamano
2017-05-08 12:27         ` Yubin Ruan
2017-05-08  6:07           ` Junio C Hamano
2017-05-08 14:10             ` Yubin Ruan
2017-05-08 12:01       ` Yubin Ruan

Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).