-2

I have stage all local changes. What do git want from me?

git version 2.31.1

enter image description here

error: Your local changes to the following files would be overwritten by checkout

Eugen Konkov
  • 22,193
  • 17
  • 108
  • 158
  • 2
    "*I have stage all local changes.* But not committed. Commit, then checkout. – phd Apr 24 '21 at 13:30
  • @phd: They are staged, so are in safe place, so they will not be lost. So it is ok to overwrite – Eugen Konkov Apr 24 '21 at 16:37
  • Checkout overwrites both files and the stage (index) so no, it's not safe. – phd Apr 24 '21 at 16:42
  • 2
    You are in detached head mode! How did _that_ happen? It looks to me as if you were trying to do something tricky and got tricked in turn. What were you trying to do? – matt Apr 24 '21 at 22:45
  • 1
    Downvoted for showing us a picture of code (the command-line conversation with Git). This is text; just copy and paste into the question, as text. @phd if you somehow filter the nasty picture of code (or just squint), you will discover we are in detached head mode. So the question now arises of why, and what the OP was intending to do. A whole new can of worms... – matt Apr 24 '21 at 22:51
  • @matt: 1. Text has not colors, picture - have. 2. I do not want my filenames to be indexed, but I do not want to change original message, so picture is by intention. Error message copied separately for index purpose. – Eugen Konkov Apr 26 '21 at 09:55

2 Answers2

3

[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.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Of course, if you _do_ want to lose the current contents of this file and let `dev` replace them, that's fine too, and Git will tell you how to do it. – matt Apr 24 '21 at 23:06
2

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

SwissCodeMen
  • 4,222
  • 8
  • 24
  • 34