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 does Git handle branches compared to SVN?
  • Getting Beyond Compare to Diff 2 Branches Simultaneously
  • how to remove changes since last git commit
  • new git repository and already git reset does not work
  • How do I move my git repo down level in the directory tree?
  • How to detect differences between local repo and remote repo in git?
  • $ 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?

  • git svn gatekeeper repository
  • git merge ignores deleted files, existing on merged branch (renamed files)
  • Converting 'CVS era' repo into git repo's with submodules
  • How to ensure database changes can be easily moved over DVCS using django
  • Create a new git repo off of a branch in a directory
  • Git for Mac OS X 10.4
  • 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.