I want to switch the branch. but get the error
error: Untracked working tree file 'foo.phtml' would be overwritten by merge.
If I delete this file the next file is complained about. The problem is that there is a very vast difference of files in the two branches.
This is what I tried:
$ git reset --hard HEAD
HEAD is now at 8a2a95c Initial checkin
$ git checkout otherbranch
error: Untracked working tree file 'foo.phtml' would be overwritten by merge.
$ git status
# On branch master
# Your branch and 'origin/master' have diverged,
# and have 1 and 677 different commit(s) each, respectively.
#
nothing to commit (working directory clean)
What is wrong here?
I also already tried
git clean -f -d
Take away: changing branches does not touch/change/remove untracked or checked in files. Remember that the working directory and index are not 'cleared' before the branch content is loaded into it!
Remember that 'git clean' only removes untracked files. To find out if a file is tracked or not, developers can issue the 'git status' command from within the repository. The git clean command will only delete certain files.
You face the problem that the branch you want to check out contains numerous files like foo.phtml
which are not tracked in your current branch. It seems that you can just delete the files, so try:
git checkout -f otherbranch
Just in case you do care about the files later, use
git stash -u
that will clean your working directory but in case you lose something, you can get it back from the stash. Now you can go ahead and checkout your other branch.
The other scenario is that the files are ignored in one branch and not in the other. In this case you should follow up the stash command (if git status shows something is changed) with the git checkout -f theotherbranch
.
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