问题
This question is similar to this one, but more specific.
I have a project with two branches (staging
and 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:
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:
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
回答1:
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 push
ed); or - use stash:
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.
回答2:
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.
回答3:
You can force checkout your branch, if you do not want to commit your local changes.
git checkout -f branch_name
回答4:
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.
回答5:
You can commit in the current branch, checkout to another branch, and finally cherry-pick that commit (in lieu of merge).
回答6:
If you get this when trying to check out a different branch:
my-mac:myGHProject ~$ git checkout other-branch
error: Your local changes to the following files would be overwritten by checkout:
src/main/resources/reference.conf
This means you've got some changes you need to commit on the branch you have checked out -- or you need to either wipe or stash them as most of the above points to. 19 out of 20 times I am way more likely just to commit my changes.
my-mac:myGHProject ~$ git branch
* my-local-branch
* develop
my-mac:myGHProject ~$ git status
On branch my-local-branch
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: src/main/resources/reference.conf
my-mac:myGHProject ~$ git add src/main/resources/reference.conf
my-mac:myGHProject ~$ git commit -m "updates on some config"
[my-local-branch] updates on some config
1 file changed, 131 insertions(+), 85 deletions(-)
Now that you've done that, you can check out the other branch and switch back and forth pretty easily.
my-mac:myGHProject ~$ git checkout other-branch
my-mac:myGHProject ~$ git status
On branch other-branch
my-mac:myGHProject ~$ git checkout my-local-branch
Switched to branch 'my-local-branch'
Just make sure you are both on the right branch and pushing to the right branch when you run your git push origin ${branch} command. Note: if you have your project hooked directly into Intellij, you can see that you changed your branch in the lower right hand corner of the main window.
来源:https://stackoverflow.com/questions/22424142/error-when-changing-to-master-branch-my-local-changes-would-be-overwritten-by-c