error: Your local changes to the following files would be overwritten by checkout
Solution 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.
Solution 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.
Solution 3
You can force checkout your branch, if you do not want to commit your local changes.
git checkout -f branch_name
Solution 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.
Solution 5
This error happens when the branch you are switching to, has changes that your current branch doesn't have.
If you are seeing this error when you try to switch to a new branch, then your current branch is probably behind one or more commits. If so, run:
git fetch
You should also remove dependencies which may also conflict with the destination branch.
For example, for iOS developers:
pod deintegrate
then try checking out a branch again.
If the desired branch isn't new you can either cherry pick a commit and fix the conflicts or stash the changes and then fix the conflicts.
1. Git Stash (recommended)
git stash
git checkout <desiredBranch>
git stash apply
2. Cherry pick (more work)
git add <your file>
git commit -m "Your message"
git log
Copy the sha of your commit. Then discard unwanted changes:
git checkout .
git checkout -- .
git clean -f -fd -fx
Make sure your branch is up to date:
git fetch
Then checkout to the desired branch
git checkout <desiredBranch>
Then cherry pick the other commit:
git cherry-pick <theSha>
Now fix the conflict.
- Otherwise, your other option is to abandon your current branches changes with:
git checkout -f branch
Related videos on Youtube

Manolo
Web developer. Back end - Front end PHP - Symfony Javascript - JQuery - HTML5/CSS3 - React MySQL - Neo4j Git - Subversion Apache - Nginx My freeware: MsalsasVotingBundle - Adds support for a voting system in Symfony Symfony Bundle Skeleton - Application for creating reusable Symfony bundles. Budget Request - Currency exchange REST API made with Symfony. Currency Exchange - Budget REST API made with Symfony. React SVG components - SVG components built with React Dynamic Audio Player - Audio player plugin for Wordpress Itransformer - Web application to transform images Need To Share - Wordpress plugin for sharing content Bunch Of Colors - Online web application for helping to select colors
Updated on March 12, 2022Comments
-
Manolo 9 months
This question is similar to this one, but more specific.
I have a project with two branches:
staging
andbeta
. I develop onstaging
, and use themaster
branch to fix bugs. So if I'm working on staging and I see an error, I change tomaster
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 getNo changes to commit
-
keltar over 8 yearsHave you tried
reset --hard
? If you really sure you want to discard your changes. Or use stash if you don't. -
Manolo over 8 years@keltar - No. I don't want to discard my changes. Just keep them on the working tree for a later commit
-
keltar over 8 yearsI 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. -
Manolo over 8 years@keltar - I've worked before in this way. I don't want to commit any changes at
staging
now. -
keltar over 8 yearsPerhaps 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.
-
Manolo over 8 years@keltar - A detailed answer would be appreciated.
-
glerYbo over 6 yearsPossible duplicate of How to ignore error on git pull about my local changes would be overwritten by merge?
-
-
Manolo over 8 yearsThank 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 :-/
-
keltar over 8 yearsOops, mistyped
add
when it is actuallysave
.. 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. -
Manolo over 8 yearsThe thing would be save all modified files except the one I want to add to
master
branch. Also, an option would bepop
the changes on other branch? -
keltar over 8 yearsI'm not sure what you mean. Yes, you can apply stash on another branch, but it will simply replace files contents, not merge them.
-
Manolo over 8 yearsPerfect! Worked like a charm. Thank you ;-)
-
CodyBugstein almost 8 yearsWhat if you want to view where these changes are?
-
keltar almost 8 years@Imray what do you mean 'where'? There is
git stash show
, which may (or may not) be what you wanted. -
CodyBugstein almost 8 yearsI mean, I want to know where in my file I made changes that is causing this prompt
-
keltar almost 8 years@Imray
git status
orgit diff
. Problem caused only by uncommitted changes, which is exactly what this commands will show. -
lukyer about 7 yearsWhen my git got jammed (no local changes but still that error), this solution helped me!
-
glerYbo over 6 yearsThe
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. -
mfaani over 6 yearsI don't get why this error has occurred. Isn't the whole concept of having branches to have separatation. I just did
git checkout master
and I got this error, which doesn't make any sense. what does master have to do with any other branch? -
keltar over 6 years@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. -
Owl about 6 yearsThanks, you saved my screen from getting a fist through it.
-
Jacek Dziurdzikowski almost 5 yearsI lost my changes that way
-
Jacek Dziurdzikowski almost 5 yearsI lost my changes that way
-
Alexander Mills over 4 yearsYes you will lose changes by doing this, this should come with a big caveat.
-
Romain Valeri about 4 years@JacekDziurdzikowski So you lost your changes twice (see comment on kiki_yu's answer), both by applying solutions that very explicitly mentionned that discarding local changes was the very purpose. Is my sarcasm detector broken or... you're serious?
-
Jacek Dziurdzikowski about 4 years@RomainValeri Hmm, I guess that was my way of warning others which are begginers with git (they have to be beginners if reading this post) to be ready to say goodbye to any changes they made. I thought that time that changes done in one branch should stay on that branch until I checkout it again. Hint to newcomers who think that way too: use git stash :)
-
Shashaank V V over 3 yearsWorked like a charm. before this i used to back my changes up in notepad, stash the original, and replace it after switching branches. this answer made more sense and is clean and easy way to handle commits. :)
-
jgmjgm over 3 yearsI want it the otherway around. master is behind my branch and I'm up to date with master but it's still unable to switch branches. Must be a git bug.
-
Philip Rego over 3 yearsIncomplete answer. Downvote. The first command can give error. "No local changes to save"
-
Sean Munson almost 3 yearsNot the answer. Why? What if I don't want the changes? What if the changes are whitespace and invisible?
-
MAChitgarha almost 3 yearsDuplicate answer, for no reason. The first answer has even more information.
-
MAChitgarha almost 3 yearsIt can be more helpful if you give more explanation on this.
-
mloning almost 3 yearsUse
git stash drop <stash_id>
to delete specific stash orgit stash clear
to delete all of them. -
Animesh Kumar about 2 yearsI lost my local changes that way
-
Ali Raza almost 2 yearsSo if we again
git stash save your-file-name
, then it will remove previous stash or merge both of them? -
keltar almost 2 years@AliRaza it'll create new stash entry. You can view them via
git stash list
. -
mLstudent33 about 1 yearif this problem occurred when I want to checkout master for the sole purpose of merging the current branch I am on, can I just merge master into the current branch and rename it master?
-
mLstudent33 about 1 yearactually upon trying this, I lost many changes and now my app does not work again.
-
mLstudent33 about 1 yearomg, I can't believe this. I lost two days of work. Strictly sticking to the Pro Git book from now and forget about branching, I'm going old school creating new files and copy pasting and then copy pasting back.