Git is allowing branch changes even with commit-free changes

Asked

Viewed 81 times

0

Usually when I work with git, I make some changes to one branch and try to check out another without committing it displays the message:

Changes not staged for commit:
  (utilize "git add <arquivo>..." para atualizar o que será submetido)
  (use "git restore <file>..." to discard changes in working directory)

Or something similar...

But after I’ve installed git again, it’s no longer restricting projects. Is this a setup? I’m using git version 2.25.1 in Ubuntu

  • Right, I didn’t quite understand the error. Tu ta be able to add the files with git add? Can do commit? Already tried to make one git init?

1 answer

1

[...] make any changes to a branch [...]

First, if you made these changes and didn’t even do the commit nor the add, means you haven’t changed into any branch. It’s only in your local directory.

When switching branches, Git checks whether the changes contained in this exchange influence files that have been modified in your local directory. If it doesn’t, the checkout is done smoothly. If it does, it asks you to commit, stash, or reverse.

The error message is as follows:

$ git checkout master
error: Your local changes to the following files would be overwritten by checkout:
        arquivo.txt
Please commit your changes or stash them before you switch branches.
Aborting

Or, if you have no problems switching branches, the message indicates that your local changes were taken in the branch exchange:

$ git checkout master
Switched to branch 'master'
M       arquivo.txt

I believe that although the message may be different between versions, the general behavior is the same.

For further clarification, read the documentation, or see this answer (in English).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.