When is switch branch using Git, the files in my project directory do not reflect the branch I switched to. Let's say I am working on a branch called "feat/some-awesome-feature" and that I created 2 new files and modified one in that branch. While still being in that branch I see those new files and the one modification of course in my directory/editor. My problem is that when I switch to another branch (ie. master), I still see those 2 files and the modification in the other file. Question: is it possible to have my directory reflecting the file structure of the branch I am currently on? By the way I use SublimeText3 with the SFTP Bond plugin which I use to connect to my vagrant.
4 Answers
A branch is just a name for a commit.
From your description, you created some files and modified others then switched the branch without committing the changes. They are still present in your working tree. Switching the branch (checking out a different commit) did not affect them because they are not in any branch. And this happens because they are not part of any commit.
You have to add the new files and updated files to the repository then commit in order to make them belong to a branch:
$ git add .
$ git commit -m "created some new files"
Then, when you checkout a different branch, the working tree will reflect the status of the project when that commit was created.
When in doubt, run git status
to see if there are changes in the working tree that are not committed.

- 68,258
- 9
- 99
- 134
-
In this case you should reword your question. You focused too much on Git and got answers about Git. Apparently the culprit is the SFTP plugin but you mentioned it only as a minor detail. I don't see in your question where you mention that the directory not updated is remote. – axiac Apr 11 '17 at 16:08
Here the problem isn't that the directory isn't switching but that the Sublime SFTP isn't updating the files correctly.
First: check the "monitoring_frequency" setting in Preferences -> Package Settings -> SFTP -> Settings-Default and make sure that this is set low enough to constantly be updating.
If you are using the entire Directory Copy: The fastest way (but gets old fast) is go to Project -> Refresh Folders. This will update the entire local directory with the changes that git has done on the remote.

- 144
- 5
There are two ways.
One is to commit the changes into feat/some-awesome-feature and checkout the other branch afterwards.
Or you git stash (git add $FILES; git stash) your changes and then checkout the other branch.

- 196
- 1
- 8
Generally the behavior you're saying you want is what git does. A couple things could be happening...
The new files: it sounds like maybe they are yet tracked (git add
). If a file is untracked then git will not disturb it when checking out another branch.
But it sounds like you also have changes to existing files. If those aren't committed, then git should resist attempts to change branches (warning that changes to such-and-such file would be overwritten).
So what to do?
Well, if you're ready to add
and commit
your changes to the branch, you can do that, and then checkout should behave as expected.
If you're not ready to commit them, then use git stash
. This puts them in a temporary commit, which you can later retrieve with git stash pop
. If you're going to start using stashes, better read the documentation: https://git-scm.com/docs/git-stash

- 42,148
- 4
- 35
- 52