2

So I created a new branch list from master.

git:(master) git co -b list
Switched to a new branch 'list'

Then I made some changes in a file while still in list branch. Then I changed back to master, and opened that file. It shows my changes there as well. Is this expected?

Maybe I am not clear how branching works, but my perception was that it creates duplicate of parent branch(master in this case), then we make modifications in branch(which are not reflected in master) then we merge. Parent(or master) stays as it is.

Please correct me if I am wrong.

software_writer
  • 3,941
  • 9
  • 38
  • 64
  • Local changes are kept until you `commit` them. So yes, valid behaviour from your description. – Marvin Feb 13 '16 at 23:13

2 Answers2

5

It shows my changes there as well. Is this expected?

Yep.

enter image description here

The working Directory and the index/stage area are shared at repository level, so unless you commit your changes they will "continue to follow" you regardless on which branch you are in right now (See the image at the end of answer for demo).

Only once you commit them they you can switch to "clean" working directory +clean index.

This is the way git behave.


Maybe I am not clear how branching works, but my perception was that it creates duplicate of parent branch(master in this case)

Git does not create any duplications, When you create branch its simply store a reference to the commit it under a new name located under your .git/ref/heads. The file with the branch name simply contains the SHA-1 which was the previous HEAD in your case.


For example: When you start to work and you suddenly realize that did not ment to work on the current branch but you wish to create new branch instead all the changes are "moving" with you to the new branch. Git store its data inside the repository. When you switch branches you set a new HEAD but the working dir + staging area stays the same.


Here is a demo - creating some files and then switching to a different branch. you can see that the files are "visible" in the new branch as well.

enter image description here

Community
  • 1
  • 1
CodeWizard
  • 128,036
  • 21
  • 144
  • 167
1

You need to commit the changes first. Git allows you to move between branches with uncommitted changes in your working directory. When this happens, git will try to reapply the uncommitted changes into the new branch.

Git only allow you to move uncommitted changes if it can do it safely, git will prevent you from moving branch if it cannot automatically reapply the changes; in the case that the file with uncommitted changes has changes when moving to another branch, you can "git stash" your uncommitted changes in that case and merge the uncommitted changes.

This behavior is often useful if you start making changes in one branch then realized you actually want to commit them into a different branch or if you have a set of changes that you want to test on multiple branches.

Lie Ryan
  • 62,238
  • 13
  • 100
  • 144