Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I switch git branches when there is an untracked file error?

Using SourceTree when I try to switch back to master from a feature branch I created I get an error:

Updating the following directories would lose untracked files in it

I want to switch back to master to make another feature branch but am unable to do so.

like image 898
primetimejas Avatar asked Mar 13 '23 11:03

primetimejas


2 Answers

Actually figured this out myself. By using the terminal in SourceTree, I entered the following command:

git checkout -f master

The -f flag forces it to switch even if there is an error. It still showed the error before switching but it did switch and I was able to make a new feature branch.

like image 180
primetimejas Avatar answered Mar 16 '23 00:03

primetimejas


Make sure your file is added to the index

git add <filename>

Then:


Commit your changes

git commit

Or stash them

git stash

You can access the stashed changes later by this:

git stash pop
like image 45
Petr Hejda Avatar answered Mar 15 '23 23:03

Petr Hejda