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:
$ 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?
In Git, branches are a part of your everyday development process. Git branches are effectively a pointer to a snapshot of your changes. When you want to add a new feature or fix a bug—no matter how big or how small—you spawn a new branch to encapsulate your changes.
A Git branch is essentially an independent line of development. You can take advantage of branching when working on new features or bug fixes because it isolates your work from that of other team members. A git branch is an independent line of development taken from the same source code.
They're unnecessary. In most cases, branches, especially branches that were related to a pull request that has since been accepted, serve no purpose. They're clutter. They don't add any significant technical overhead, but they make it more difficult for humans to work with lists of branches in the repository.
when you switch to a branch without committing changes in the old branch, git tries to merge the changes to the files in the new branch. If merging is done without any conflict, swithing branches will be successful and you can see the changes in the new branch.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With