Git: branches should isolate the changes, or should not?

I thought I had already a decent grasp of Git, but just now I’ve been surprised. I thought that branches would isolate the changes from other branches, so I started a big experimental refactoring in a new branch. This refactoring, implied moving many files. When I switched to master I could still see the changes though!

I went to my sandbox repository to replicate the problem:

  • How can developers use Git with no shared network access?
  • How do you use Git within Eclipse as it was intended?
  • Why git operations becomes slow when repo gets bigger
  • Why shouldn't I push a virtualenv to Heroku?
  • How to require confirmation for git reset --hard?
  • Setting NODE_ENV variable in elasticbeanstalk
  • $ git branch crazy-refactoring
    $ git checkout crazy-refactoring
    $ rm README
    $ git checkout master
    $ git status
    # On branch master
    # Changes not staged for commit:
    #   (use "git add/rm <file>..." to update what will be committed)
    #   (use "git checkout -- <file>..." to discard changes in working directory)
    #
    #       deleted:    README
    #
    no changes added to commit (use "git add" and/or "git commit -a")
    $ ls README
    ls: cannot access README: No such file or directory
    

    Why? The whole point of me creating a branch, was that I could try something, and throw it away if unsuccessful. Toughts?

  • Character encoding with msysgit
  • How do you use an existing completion for a function in zsh?
  • ignore all _notes directories with GIT
  • Git add on modified file not working, except with -p (patch)
  • How to 'git pull' into a branch that is not the current one?
  • Is it possible to 'fork" a repo upto a specific commit?
  • One Solution collect form web for “Git: branches should isolate the changes, or should not?”

    You did not commit the delete. Only committed changes are “isolated”.

    Regarding your comment:

    Maybe I’m missing something, but it seems to me a weird and undesirable behavior.

    Think about it:

    You deleted README in crazy-refactoring but did not commit the change. Git knows that you changed the file and that you did not commit it. In order to preserve your changes, it does not override them if you checkout another branch.
    Otherwise there would be no way for Git to know what the changes have been when you switch back to branch you made the changes in. They would be lost, and that is far more worse than seeing the changes in the other branch.

    You current working directory is always current branch + uncommitted changes (unless you reset them).

    Of course, Git could warn you when checking out the other branch, that you have uncommitted changes, but sometimes it is desired to keep the changes and switch to another branch.

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