219

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

ouflak
  • 2,458
  • 10
  • 44
  • 49
Manolo
  • 24,020
  • 20
  • 85
  • 130
  • 4
    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
  • @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
  • 1
    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
  • 1
    @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
  • 1
    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
  • @keltar - A detailed answer would be appreciated. – Manolo Mar 15 '14 at 14:00
  • Possible duplicate of [How to ignore error on git pull about my local changes would be overwritten by merge?](http://stackoverflow.com/questions/14318234/how-to-ignore-error-on-git-pull-about-my-local-changes-would-be-overwritten-by-m) – kenorb Mar 16 '16 at 12:44

10 Answers10

266

Warning: Running this will discard local changes. Only run this if you want to discard local changes.

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.

busterroni
  • 591
  • 8
  • 17
KikiYu
  • 3,087
  • 1
  • 13
  • 14
189

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:
    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.

Kashyap
  • 15,354
  • 13
  • 64
  • 103
keltar
  • 17,711
  • 2
  • 37
  • 42
  • 3
    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
  • 1
    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
  • 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
  • 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
  • Perfect! Worked like a charm. Thank you ;-) – Manolo Mar 15 '14 at 15:56
  • What if you want to view where these changes are? – CodyBugstein Mar 01 '15 at 14:21
  • @Imray what do you mean 'where'? There is `git stash show`, which may (or may not) be what you wanted. – keltar Mar 01 '15 at 14:40
  • I mean, I want to know where in my file I made changes that is causing this prompt – CodyBugstein Mar 01 '15 at 15:06
  • @Imray `git status` or `git diff`. Problem caused only by uncommitted changes, which is exactly what this commands will show. – keltar Mar 01 '15 at 15:10
  • I 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? – mfaani Aug 19 '16 at 14:45
  • 1
    @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
  • Worked 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. :) – Shashaank V V Mar 15 '19 at 06:47
  • Incomplete answer. Downvote. The first command can give error. "No local changes to save" – Philip Rego May 17 '19 at 21:55
  • Not the answer. Why? What if I don't want the changes? What if the changes are whitespace and invisible? – Sean Munson Jan 23 '20 at 19:50
  • Use `git stash drop ` to delete specific stash or `git stash clear` to delete all of them. – mloning Jan 31 '20 at 09:30
  • So if we again `git stash save your-file-name`, then it will remove previous stash or merge both of them? – Ali Raza Feb 18 '21 at 09:07
  • 1
    @AliRaza it'll create new stash entry. You can view them via `git stash list`. – keltar Feb 18 '21 at 10:41
  • if 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 Sep 24 '21 at 02:23
  • actually upon trying this, I lost many changes and now my app does not work again. – mLstudent33 Sep 24 '21 at 02:38
  • omg, 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. – mLstudent33 Sep 24 '21 at 02:57
  • if you skipped your file from worktree (git update-index --skip-worktree your_file) you have to undo that first using the same command but with --no-skip-worktree option. THEN, stash and checkout. – salouri Aug 25 '22 at 05:38
19

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

git checkout -f branch_name
SidOfc
  • 4,552
  • 3
  • 27
  • 50
Deepika Patel
  • 2,581
  • 2
  • 19
  • 13
  • 1
    The `sudo` is not necessary, it'll only break the file permissions. It's the same git command as posted by [@kiki_yu](http://stackoverflow.com/a/32556166/55075) one year before, but it's even worse. – kenorb Jul 24 '16 at 12:10
  • 2
    I lost my changes that way – Jacek Dziurdzikowski Feb 20 '18 at 11:40
  • 2
    @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? – Romain Valeri Nov 22 '18 at 14:42
  • 1
    @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 :) – Jacek Dziurdzikowski Nov 22 '18 at 23:03
  • Duplicate answer, for no reason. The first answer has even more information. – MAChitgarha Jan 23 '20 at 22:29
11

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.

BeNiza
  • 615
  • 6
  • 4
10

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.

  1. Otherwise, your other option is to abandon your current branches changes with:
git checkout -f branch
ScottyBlades
  • 12,189
  • 5
  • 77
  • 85
1

i had got the same error. Actually i tried to override the flutter Old SDK Package with new Updated Package. so that error occurred.

i opened flutter sdk directory with VS Code and cleaned the project

use this code in VSCode cmd

git clean -dxf

then use git pull

1

The explanation in the accepted answer from Keltar is very good (and I have upvoted it), but I want to give a different answer because I do not think it is accurate. What git considers is not really if there are changes on the modified files on the target branch / commit since last merge-point. It's all dependent on if the modified files are the same between HEAD and the target commit. If the files are the same, git has no problem checking out and keeping the files as you have them in the working tree. If, however, at least a modified file is not the same between HEAD and the target commit / branch then git refuses to checkout because it would require a merge to be able to move to that place. Which, somebody might say, is the same as what it says in the accepted answer, right? Well, not really. A file can be the same (content) and still have commits in the target branch history under many different circumstances... like:

  • There was a commit and then a revert on the file on the target branch history.
  • The same change is applied on both histories.

And that is without considering cases like moving backward/forward in history. So, remember: HEAD vs target commit.

eftshift0
  • 26,375
  • 3
  • 36
  • 60
0

You can commit in the current branch, checkout to another branch, and finally cherry-pick that commit (in lieu of merge).

Vitaly Zdanevich
  • 13,032
  • 8
  • 47
  • 81
0

İf you guys are using taskrunner, you should stop it, make the git changes and then run the tasrunnner again. Otherwise, the taskrunner watches and changes the tracking files which you are changing with git comments.

ouflak
  • 2,458
  • 10
  • 44
  • 49
0

In my case, it was something different.

I've created a commit and push it, later I did git update-index --assume-unchanged <file> and edited the content to use in local but when I want to checkout to another branch git prompts me this error.

My solution was undo the local change and checkout to branch needed and do the local change there.

abendevs
  • 136
  • 1
  • 11