Error when changing to master branch: my local changes would be overwritten by checkout

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP

Error when changing to master branch: my local changes would be overwritten by checkout



This question is similar to this one, but more specific.



I have a project with two branches (staging and beta).


staging


beta



I develop on staging, and use the master branch to fix bugs. So if I'm working on staging and I see an error, I change to master branch:


staging


master


master


git checkout master



and do the stuff:


git add fileToAdd
git commit -m "bug fixed"



and then I merge with both branches:


git checkout staging
git merge master
git checkout beta
git merge beta



And doesn't matter if there are other files on the working tree.



But now, when I try to change to the master branch, I'm getting an error:


master


error: Your local changes to the following files would be overwritten by checkout:
src/Pro/ConvocationBundle/Controller/DefaultController.php
Please, commit your changes or stash them before you can switch branches.
Aborting



I thought that I should remove the file from the staging area:


git reset HEAD src/Pro/ConvocationBundle/Controller/DefaultController.php



but I'm getting the same error. If I do git status I get No changes to commit


git status


No changes to commit





Have you tried reset --hard? If you really sure you want to discard your changes. Or use stash if you don't.
– keltar
Mar 15 '14 at 13:34


reset --hard





@keltar - No. I don't want to discard my changes. Just keep them on the working tree for a later commit
– Manolo
Mar 15 '14 at 13:36





I don't think you can switch branches while keeping uncommitted changes, but i could easily be wrong - not really my field. Try git add your-file and commit.
– keltar
Mar 15 '14 at 13:39


git add your-file





@keltar - I've worked before in this way. I don't want to commit any changes at staging now.
– Manolo
Mar 15 '14 at 13:46


staging





Perhaps your conflicting file wasn't changed when you tried that before. You have changes, git have to save them somewhere to restore later. It is very unlikely to be possible without commits. But if you really don't want to - use stash, it is exactly why it exists.
– keltar
Mar 15 '14 at 13:50




4 Answers
4



Your error appears when you have modified a file and the branch that you are switching to has changes for this file too (from latest merge point).



Your options, as I see it, are - commit, and then amend this commit with extra changes (you can modify commits in git, as long as they're not pushed); or - use stash:


push


git stash save your-file-name
git checkout master
# do whatever you had to do with master
git checkout staging
git stash pop



git stash save will create stash that contains your changes, but it isn't associated with any commit or even branch. git stash pop will apply latest stash entry to your current branch, restoring saved changes and removing it from stash.


git stash save


git stash pop





Thank you. Are you sure that this won't do any changes on my working tree (not added files)? I don't want to loose my changes :-/
– Manolo
Mar 15 '14 at 15:17





Oops, mistyped add when it is actually save.. updated. You mean, for other files? git stash save without file name parameter will save all modified files, if you want to (and revert them to latest-commited state). And having extra copy of directory tree never hurts, but i'm always paranoid about it.
– keltar
Mar 15 '14 at 15:24


add


save


git stash save





The thing would be save all modified files except the one I want to add to master branch. Also, an option would be pop the changes on other branch?
– Manolo
Mar 15 '14 at 15:40



master


pop





I'm not sure what you mean. Yes, you can apply stash on another branch, but it will simply replace files contents, not merge them.
– keltar
Mar 15 '14 at 15:45





@Honey it have nothing to do with branches, problem is uncommitted changes. Checkout, by definition, have to reset your files to the state of master, but by doing so it will lose it's current contents, and since this contents aren't committed it would be impossible to return to this state later, hence an error so you wouldn't be upset about lost changes later.
– keltar
Aug 19 '16 at 15:46


master



I encountered the same problem and solved it by


git checkout -f branch



and its specification is rather clear.



-f, --force



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.





When my git got jammed (no local changes but still that error), this solution helped me!
– lukyer
Nov 19 '15 at 13:17





Thanks, you saved my screen from getting a fist through it.
– Owl
Sep 7 '16 at 14:25





I lost my changes that way
– Jacek Dziurdzikowski
Feb 20 at 11:39





Yes you will lose changes by doing this, this should come with a big caveat.
– Alexander Mills
Jul 15 at 1:32



You can force checkout your branch, if you do not want to commit your local changes.


git checkout -f branch_name





The sudo is not necessary, it'll only break the file permissions. It's the same git command as posted by @kiki_yu one year before, but it's even worse.
– kenorb
Jul 24 '16 at 12:10


sudo





I lost my changes that way
– Jacek Dziurdzikowski
Feb 20 at 11:40



I encountered the same problem and solved it by



git checkout -f branch



Well, be careful with the -f switch. You will lose any uncommitted changes if you use the -f switch. While there may be some use cases where it is helpful to use -f, in most cases, you may want to stash your changes and then switch branches. The stashing procedure is explained above.


-f


-f


-f


stash


switch


stashing






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

Executable numpy error

PySpark count values by condition

Mass disable jenkins jobs