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:

  • Cannot push to Heroku 'fatal: unable to access..Could not resolve host: (nil); nodename nor servname provided, or not known'
  • How to get back to most recent version in Git?
  • Mysysgit “(END)” displays, how do I proceed?
  • Why can't I do git diff on this specific commit?
  • How do you push just a single Git branch (and no other branches)?
  • Git workflow for custom css files and themes
  • $ 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?

  • Unable to Run Build on Hudson Using Git
  • How does a Git repository works with a develop and master branch?
  • Git file permissions on Windows
  • ssh key passphrase works in windows but not in linux
  • How to remove a too large file in a commit when my branch is ahead of master by 5 commits
  • Cannot push to remote git repo over ssh, stuck at “Writing objects: 11%”
  • 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.