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

Re: Git branches - confusing behavior

From
Björn Steinbrink <b.steinbrink@gmx.de>
Date
May 11, 2008, 13:27 UTC
Message-ID
<20080511132752.GA22778@atjola.homenet>
In-Reply-To
<4826EEDF.4010404@gmail.com>
On 2008.05.11 16:04:31 +0300, Dima Kagan wrote:
Show 15 quoted lines
> Björn Steinbrink wrote:
> > On 2008.05.11 15:33:21 +0300, Dima Kagan wrote:
> >>>> Basically I see that the same file I edited on the 'test_branch'
> >>>> branch appears to be modified on the 'master' branch as well. This
> >>>> behavior is unwanted, of course.
> >>>>
> >>>> Can someone please tell me, what am doing wrong? Or is this git's
> >>>> normal behavior?
> >> I just realized that this behavior is even more confusing.  If I
> >> commit the file on 'test_branch' and only then 'git checkout master'
> >> the changes are not visible on 'master' until I merge. So why should
> >> 'master' be affected by uncommitted changes on some branch???
> > 
> > Uncommitted changes are not on any branch, they are in your working tree
> >
[example removed]
Show 7 quoted lines
> > 
> > 
> > The uncommitted changes survived the branch change and are still in the
> > working tree, in svn just like in git.
> 
> Yes, I am aware of that, except one rarely works in one directory on
> multiple svn branches, because the branches are not private.

I always did that, the working copy _is_ private even in svn, so messing with that was not a problem there either.

> Git's branches can be private, so perhaps this behavior should be
> different from SVN?

No. Uncommitted changes are, well, uncommitted. They don't belong to any branch yet. A branch is not some structure that contains history in itself. A branch just points to a commit, and the commits, with their parent-child relations, form the actual history. The index and working tree are not part of a branch.

Changing that would even break a workflow that is rather common for me. I start working on something that is either just experimental or assumed to be a very small change. Then I realize that the change is worth keeping and/or too big and deserves its own branch. At that point, I can just do "git checkout -b new_branch", and pretend that I started working on that branch right from the start. With your proposed change, I would need some extra command to transfer the work in progress from the old branch to the new branch.

If I ever want to switch to another branch and not keep the changes in my working tree and index, I stash them away or create a temporary commit, which I later amend. That's a use-case that comes up rather seldom though (for me at least).

> BTW, Is there a way to do 'svn checkout -b new_branch' into a new directory?

There's a script (new-working-tree or so) in contrib/ that can create a second working tree for a given repo, but I usually prefer to just clone my repo locally. In the second repo, I can then switch branches as much as I like ;-)

Björn
Previous: Dima KaganNext: Dima Kagan
Message 13 of 19 in “Git branches - confusing behavior”
  1. Dima KaganMay 11, 2008
  2. Jakub NarebskiMay 11, 2008
  3. Dima KaganMay 11, 2008
  4. David SymondsMay 11, 2008
  5. Dima KaganMay 11, 2008
  6. David SymondsMay 11, 2008
  7. Dima KaganMay 11, 2008
  8. Steve FrécinauxMay 11, 2008
  9. Jakub NarebskiMay 11, 2008
  10. Dima KaganMay 11, 2008
  11. Björn SteinbrinkMay 11, 2008
  12. Dima KaganMay 11, 2008
  13. Björn SteinbrinkMay 11, 2008
  14. Dima KaganMay 11, 2008
  15. Teemu LikonenMay 11, 2008
  16. Miles BaderMay 12, 2008
  17. Theodore TsoMay 11, 2008
  18. Dima KaganMay 11, 2008
  19. Patrick AljordMay 11, 2008

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.