I have stage all local changes. What do git want from me?
git version 2.31.1
error: Your local changes to the following files would be overwritten by checkout
I have stage all local changes. What do git want from me?
git version 2.31.1
error: Your local changes to the following files would be overwritten by checkout
[NOTE Things are made more complicated here by the fact that you have somehow gotten yourself into "detached head" mode. You have not explained how that happened, and getting yourself out of it is not at all trivial. A complete answer would require more information so that we can talk about that. Instead, I'll just talk about the warning from Git.]
Git is absolutely right to warn you, loudly. The reason is obvious if you know what switching branches means. What does git checkout dev
actually do?
Ground of being: The files you can actually see are not yours. They belong to Git, which lends them to you so that you can edit, add, and commit. They constitute the repository's working area (or working tree).
Okay, now, if you switch to dev
, all the files in the dev
commit are copied out into your working area (as well as into the index). That is what checkout
means. So the files you can see are going to be (more or less) completely replaced by a different set of files.
Okay, fine. But you have uncommitted work sitting in your working area, namely, you've got this new file, AgreementTotals.pm. But at the same time, a file by that pathname, with different contents, already exists in dev
! (You can see the difference, by saying git diff dev
.)
So if you now switch to dev
and the contents of the working area are rewritten, what's going to happen to this version of AgreementTotals.pm? If the switch happened right now, you would lose your work on this AgreementTotals.pm. Is that really what you want?
What do git want from me?
Git wants you to commit just before you switch branches. You can do this the straightforward way, by saying git commit
, or you can do it a sort of sneaky way, which I do not recommend, by saying git stash
.
(A complication is added by the fact that you are in detached head mode. You cannot commit because you are not "on a branch". Git warned you about that, too, when you checked out the detached head. You would need to create a branch, right now, in order to commit onto it, if you want to save your current work on AgreementTotals.pm.)
So, in sum, Git is absolutely right to stop you here. And consider yourself lucky. Every once in a while, I read of horror stories where Git did not warn the user, but just went ahead and replaced the contents of the working area, and the user lost a bunch of edits that had not been committed. That's not supposed to happen, but it can. Make it a habit to commit before switching and your life will be so much better.
From my experience git tells you exactly what's wrong, if you git checkout
without committing the changes, they would be overwritten (deleted). In case you don't want to commit these changes just yet, another option would be to git stash
them.
Here's a link to a more detailed answer Checkout another branch when there are uncommitted changes on the current branch