Reputation: 989
In my git repo master branch i have working dir /tmp/repo_name/secret_code. If i switch to new branch called test and then rm -rf secret_code
.. when i change back to master it deletes the secret_code directory from there as well.
I thought if i make changes on a git branch that it acts as a container for changes so on changing to another branch the working dir will reflect that branches state.
Why when i remove files from a branch are those actions reflected on my master branch ?
Thank you.
Upvotes: 4
Views: 833
Reputation: 3821
VonCs answer is correct, but perhaps it will help you to get a more thorough explanation what is going on.
A branch in git is basically just a pointer to a commit in the repository. You said that you're on the master branch, and then switch to a new branch. That new branch will start out pointing to the same commit as the master branch then. When you do a commit while on that new branch, the branch will be automatically updated to point to the new commit, while the master branch will remain unchanged.
When you change code in the working tree though, that isn't really part of the branch yet. Git just considers this as something you did on top of the current HEAD commit, but it won't add it into the actual repository until you tell it to (by e.g. committing, or perhaps stashing).
Now you switch back to the master branch while you still have uncommitted changes in the working tree. What should git do? You apparently expect git to make the working tree look exactly like the tip of master that you are checking out, so it would restore the secret_code directory in your case. However, as I said before those changes are not in the repository yet. The changes would be lost. I think you can imagine that that might cause a lot of pain for the user if he didn't realize he still had uncommitted changes and that those would be overwritten...
So instead, git will try to keep your working tree changes. IIRC, it will simply refuse to check out the other branch if you changed a file which would also have to be modified by checking out the new branch, and will tell you to commit, stash or revert the changes first. But in your case, it was just able to keep the change so that the secret_code directory stays deleted in your working tree.
But just as the change wasn't part of the new branch yet, it also isn't part of the master branch yet. That would only happen if you actually commit the change while you're on the master branch.
Upvotes: 3
Reputation: 1327184
Why when i remove files from a branch are those actions reflected on my master branch ?
Because you modified the working tree: when you switch back to master
, if master
didn't have modifications done on secret_code
, it will keep the working tree as is, and the git status
will reflect the deletion of that folder.
This would remove secret_code
only in test
branch:
git checkout test
git rm -r secret_code
git add -u .
git commit -m "removes secret_code in test"
Then:
git checkout master
You would still see secret_code
in that master
branch.
Upvotes: 4