Git branch is not working as I am expecting

Okay so I am new to using git..and I thought I understood the concept of branching, but now I am confused.

Lets say I have a master.

  • GIT: How do I add a file to the first commit (and rewrite history in the process)?
  • How to setup github repository in my NetBeans IDE?
  • tracking daily changes, as a peon, in a clearcase shop
  • Scripting git commands in Python without external library
  • TeamCity Github one project works, the other does not
  • How do we run git status for a specific remote?
  • I create a branch called branch1.

    git checkout -b branch1

    I made some changes on branch1, but I realized I have to do something else before finishing my work on branch1.

    So I go back to master

    git checkout master

    and create a new branch called branch2.

    git checkout -b branch2


    Since I didn’t commit my changes on branch1, I am expecting my branch2 will not have branch1 codes…but I am seeing the changes made from branch1.

    Am I misunderstanding something?

  • Yet another “how to rebase one Git repo onto another” issue
  • git submodules: ln -s
  • Git: How to maintain two branches of a project and merge only shared data?
  • git problem with getting previous file version
  • Delete Folder from gitHub through eclipse
  • IntelliJ Whitespace formatting
  • 2 Solutions collect form web for “Git branch is not working as I am expecting”

    Although you were in branch1 when you made the changes, those changes are not really “in” branch1 until you commit them.

    From $ git help checkout:

    Local modifications to the files in the working tree
    are kept, so that they can be committed to the <branch>.

    If, on the other hand, you commit your changes to branch1 before switching back to master, then when you $ git checkout master, git will revert the files in your working directory back to the snapshot that master points to, as you are expecting.

    If for some reason you really want to avoid committing your changes to branch1, but still work on a different branch without those changes, you might consider using $ git stash to stash your changes, then switch to your new branch and do the work you need to, then switch back to branch1 and do $ git stash pop to get back your stashed changes.

    Since I didn’t commit my changes on branch1, I am expecting my branch2 will not have branch1 codes

    Working copy (staged + unstaged changes) is the same for all branches. And what you see is not the branch1 changes, but the working copy changes.

    Once you commit your changes in branch1, you’ll no longer see them in branch2 or any other branch.

    Git Baby is a git and github fan, let's start git clone.