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

Re: Git: Unexpected behaviour?

From
Carlos Martín Nieto <cmn@elego.de>
Date
Nov 11, 2011, 21:03 UTC
Message-ID
<20111111210352.GA4752@centaur.lab.cmartin.tk>
In-Reply-To
<1321044904175-6986736.post@n2.nabble.com>
On Fri, Nov 11, 2011 at 12:55:04PM -0800, Jvsrvcs wrote:
> Unexpected git behaviour
> 
[ ... switch branches with local modifications ...]
Show 5 quoted lines
> #You will see both lines in the master.  
> 
> Question #1:
> 	Why was this line added in the *master branch?
> 

It wasn't. that line was added in the working directory. When you switch branches, if the file in the tip of the current branch and the file in the tip of the target branch don't differ, it's safe to keep your local changes, so git does. This is to support the use-case where you start editing a file when the wrong branch is checked out and want to change to the right one.

Show 10 quoted lines
> 
> --- even further surprising
> In the master branch, now do a commit
> $git commit -a
> 
> cat readme.txt ( you will see the line in the master now that was added in
> the test branch )
> 
> Question #2:
> 	Why did this happen?
Because you told git to commit the file with that modification in it.
Show 9 quoted lines
> 
> # Now switch back to the test branch
> $git checkout test
> $cat readme.txt
> 
> You will only see the one line: "This was added in the master branch"
> 
> Question #3:
> 	Why did this happen?

Because the file in the 'test' branch only has that line. As you said yourself, you edited the file but didn't commit.

> 
> and NOT the line added in that branch: "this was added in the test branch"
> <= this line is gone

Again, that line wasn't added in any branch but in the working directory. The active branch was 'test', but doesn't magically mean that uncommitted changes travel with it.

   cmn
Previous: JvsrvcsNext: Jvsrvcs
Message 2 of 14 in “Git: Unexpected behaviour?”
  1. JvsrvcsNov 11, 2011
  2. Carlos Martín NietoNov 11, 2011
  3. JvsrvcsNov 11, 2011
  4. Taylor HedbergNov 11, 2011
  5. Gelonida NNov 11, 2011
  6. Chris PackhamNov 11, 2011
  7. J.V.Nov 12, 2011
  8. Chris PackhamNov 12, 2011
  9. Junio C HamanoNov 12, 2011
  10. Martin von ZweigbergkNov 14, 2011
  11. Junio C HamanoNov 14, 2011
  12. Jakub NarebskiNov 14, 2011
  13. Alexey ShumkinNov 12, 2011
  14. Eugene SajineNov 11, 2011

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.