Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git checkout master error: the following untracked working tree files would be overwritten by checkout

I have a git repository. It has A B C D E ... commits. Now I want to checkout D as a new branch named Dbranch. So I excute:git checkout D -b Dbranch. And now I want to delete this branch. Firstly I need to switch to master branch , and then use git branch -d Dbranch to delete it. But when I excute git checkout master, it gives me the error.

error: The following untracked working tree files would be overwritten by checkout:     source/a/foo.c         ...... (too many lines) Please move or remove them before you can switch branches. Aborting 

How to delete the Dbranch?

like image 648
Fei Xue Avatar asked Aug 01 '13 08:08

Fei Xue


People also ask

How do I force checkout master in git?

Force a Checkout You can pass the -f or --force option with the git checkout command to force Git to switch branches, even if you have un-staged changes (in other words, the index of the working tree differs from HEAD ). Basically, it can be used to throw away local changes.


2 Answers

Try git checkout -f master.

-f or --force

Source: https://www.kernel.org/pub/software/scm/git/docs/git-checkout.html

When switching branches, proceed even if the index or the working tree differs from HEAD. This is used to throw away local changes.

When checking out paths from the index, do not fail upon unmerged entries; instead, unmerged entries are ignored.

like image 120
aked Avatar answered Oct 19 '22 16:10

aked


With Git 2.23 (August 2019), that would be, using git switch -f:

git switch -f master 

That avoids the confusion with git checkout (which deals with files or branches).

And that will proceeds, even if the index or the working tree differs from HEAD.
Both the index and working tree are restored to match the switching target.
If --recurse-submodules is specified, submodule content is also restored to match the switching target.
This is used to throw away local changes.

like image 39
VonC Avatar answered Oct 19 '22 17:10

VonC