Subject: Re: Git: Unexpected behaviour?

From: Jakub Narebski <[email-masked]>
Date: Mon, 14 Nov 2011 13:33:51 -0800 (PST)

Martin von Zweigbergk <[email-masked]> writes:
> On Sat, Nov 12, 2011 at 11:37 AM, Junio C Hamano <[email-masked]> wrote:
> > "J.V." <[email-masked]> writes:
> >
> > > OK so "work tree" is a new term for me.  I thought we were in isolated
> > > sandboxes called "branches" and changes made in a branch would stay in
> > > that branch regardless.

That would be the default and only solution if each branch was checked
out to a separate working directory.

You can do that in git using git-new-worktree script from contrib.

> > Do not think of "branches" as isolated _sandboxes_.
> >
> > Rather, "branches" are where the independent states are to be _recorded_.

Branches are lines of development, and are about _comitted_ changes.
This means that when switching branches "in place", un-committed
changes are not on any branch.

> I think I was confused about this when learning Git too. I friend of
> mine made the following argument, which I agree with and which I haven
> seen on the list before:
> 
> Either you want the modifications to stay on the branch, or you want
> them to carry over to the branch you are checking out. In the former
> case, you would want Git to fail if there are modifications (that you
> might have forgotten you made). In the latter case, you would want
> "git checkout -m". The current behavior is somewhere in between. It is
> not clear to me if there is a use case where the current behavior is
> better (from the user's point of view) than either failing or
> "checkout -m".

The "checkout -m" behavior is unsafe; you can land in a state where it
would be difficult to revert, and could lose your changes.  The
default behavior of switching branches is to carry over changes if it
is safe to do so.
 
> It is obviously too late to change this now, though.

Well, we could in theory add knob that would stash changes when
switching to branch, and unstash when switching to branch.

-- 
Jakub Narębski