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
YRYubin Ruan <ablacktshirt@gmail.com>
Date
May 8, 2017, 11:18 UTC
Message-ID
<20170508111836.GB3014@HP>
In-Reply-To
<xmqqzieo9hfo.fsf@gitster.mtv.corp.google.com>
On Mon, May 08, 2017 at 12:05:31PM +0900, Junio C Hamano wrote:
Show 33 quoted lines
> 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
Show 9 quoted lines
> 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

Previous: Junio C HamanoNext: Junio C Hamano
Message 3 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.