-1

I'm new in git. I got error message error: Your local changes to the following files would be overwritten by checkout: after git checkout.

And I found a good question from Stack Overflow here.

But I'm still confused. Why are my files followed to the branch that I wanted to checkout? I expected the files are remain in local branch and I could simply move to dev branch without the files.


My situation

local branch that I'm focusing(all files are committed)
- add => new_file.java
- delete => deleted_file.java
- modify => modified_file.java

git checkout dev
=> error: Your local changes to the following files would be overwritten by checkout:

Edit

I figured out what happened. Sorry, my explanation above was wrong.

admin : is local branch that I'm focusing(all files are committed)
- add => new_file.java
- delete => deleted_file.java
- modify => modified_file.java

someone removed origin/admin branch, and created origin/admin/dev branch.
- my local
ㄴadmin

- origin
ㄴadmin (removed)
ㄴadmin/dev (created)

I tried checkout origin/admin/dev branch!
git checkout origin/admin/dev

- my local
ㄴadmin
ㄴadmin/dev (checkout failed)

it may failed. because my local branch's name 'admin' and remote branch's name 'admin/dev' are crashed. if branch's name is crashed, checkout is failed and the files are remain my workspace! Using the slash character in Git branch name

Sorry for the wrong explanation.

hynuah_iia
  • 429
  • 7
  • 14

1 Answers1

1

The files will remain in the branch when you commit them. Given that they are not committed, well, normally git will move to the branch that you want to checkout if the files that are modified on the working tree are not changed between your curtent position and where you want to go. Given that is not the case, git is warning you. What can you do? You can either wrap up a new revision by committing those changes, or you can stash them so that you can come back later to the branch and continue what you are doing

git stash save -m "will come back later (or whatever you want to say)"
git checkout another-branch
# do lots of things
# when you are ready to go back:
git checkout the-branch-where-i-was
git stash pop

And you are back to where you were before.

eftshift0
  • 26,375
  • 3
  • 36
  • 60
  • thank you! your comment's helpful to me. the problem is just my first explanation was wrong :( I misunderstood the situation. – hynuah_iia Nov 14 '20 at 13:41