Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git branch switching does not change code folder files

Tags:

git

This is a follow up to this question.

I cloned the remote master project folder using git clone [email protected]:TEST command. from the Git root, I did a cd Test command to move into that folder. I then created a branch using git checkout -b myBranch. Now on the git bash it shows that i'm in the new branch. I created a test.txt file in the Test folder and then switched my branch to master. I still see the text file. Isn't Git supposed to not show the test.txt since it not part of master. Do I need to commit my changes in the branch if this has to happen.

I thought initially this may have happened because it is not part of my .net project (that I have in the master and branch). But the same happened when I tried to add a file to mybranch. When I switch my master, I still see the changed icon for the folders in the fie system.

What am I missing? Thank you for all your help as I have spent a lot of time trying to figure out Git now. Every time I think I have got it, there is always something.

like image 908
Alex J Avatar asked Mar 02 '12 02:03

Alex J


People also ask

Does switching branches change files?

When you switch branches, files that are not tracked by Git will remain untouched. Since Git does not know about new_file. dat , it will not just delete it.

How do you switch between branches and codes?

Access the "Source Control" tab on the left side of VSCode. Click on the "three small dots" next to the refresh button. Click on the "Checkout to..." option. Choose the branch you want to switch to.

What happens when you switch branches?

When you switch a branch, all files that are under control of Git will be replaced with the state of the new branch. That includes changes to files as well as additions and deletions. In your case this means that you have some files in your current 'local' branch that simply do not exist in the master.

Does git checkout change files?

The git checkout command lets you navigate between the branches created by git branch . Checking out a branch updates the files in the working directory to match the version stored in that branch, and it tells Git to record all new commits on that branch.


1 Answers

No it's not part of your master branch, but it's not part of myBranch either. It is untracked, because you never committed the file. Running git status you'll see it listed as untracked. Since it was never staged or committed, Git doesn't (and can't) manage its existence, so it stays there regardless.

Further, modified files remain between branches if they can. Else Git will give you an error when switching branches about a dirty working tree. This is, again, because Git has no internal copy of those changes, they exist only in your filesystem, and not within Git till they are committed.

like image 62
Andrew Marshall Avatar answered Oct 13 '22 19:10

Andrew Marshall