40

I have two branches locally, master and Berislav. The latter is currently active, and I have committed all the changes. When I try to checkout to master, I get the following message:

error: Your local changes to the following files would be overwritten by checkout: [list of files changed in the active branch] Please, commit your changes or stash them before you can switch branches. Aborting

However, everything else I tried -- commit, status, merge -- tells me that there's nothing to commit (working directory clean). What do I need to do to get to my master branch?

EDIT: When I try git stash, I'm getting:

error: feeding unmodified [file path] to diffcore

for all the files listed in the error above.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Berislav Lopac
  • 16,656
  • 6
  • 71
  • 80
  • http://stackoverflow.com/questions/6337122/git-branches-behaving-strangely and http://stackoverflow.com/questions/6638937/switching-branches-in-git-when-will-i-get-you-have-local-changes-cannot-switc answers might help, with http://stackoverflow.com/questions/7463392/switching-a-branch-after-aborting-current-changes-in-git as possible alternatives – VonC Jan 03 '12 at 07:13
  • 6
    What is the output of `git status`? – manojlds Jan 03 '12 at 07:28
  • +1 to manojlds. That's useful information to solve this problem. – Noufal Ibrahim Jan 03 '12 at 07:34
  • Just: # On branch Berislav nothing to commit (working directory clean) – Berislav Lopac Jan 03 '12 at 07:51
  • Which OS do you run? If windows, have you setup autocrlf? – Rudi Jan 04 '12 at 11:04
  • Some related information seem to be available here: http://stackoverflow.com/questions/6058879/what-do-the-git-pairing-broken-and-unknown-statuses-mean-and-when-do-they-o – Berislav Lopac Jul 11 '13 at 00:08

8 Answers8

41

I encountered a similar problem today. git status wasn't listing the files which checkout was complaining about. I did a:

git checkout -- path/to/file

And that undoes any changes to the file.

An even easier way to undo all unstaged changes on current working directory [1]:

git checkout -- .

[1] - Be warned - you will lose any other unstaged changes you were working on (if any). If you don't know what you are doing, then keep a backup of the files you were working on :)

Munawwar
  • 1,712
  • 19
  • 14
  • This helped me out big time. ```git checkout -f branch``` wasn't working but this did the trick. – rurp Oct 13 '16 at 14:40
  • This sometimes doesn't help. In an actual case, a file's line endings changed from CRLF to LF, .gitattributes contains `*.sh text eol=lf` but `git checkout -- .` didn't replace them by the new CR-less version – Daniel Alder Dec 19 '17 at 08:25
39

As simple as this:

git stash
git stash pop

The "errors" you see when running git stash aren't anything to be concerned about. It's just git recognizing that the file doesn't have any uncommitted changes.

user697576
  • 780
  • 1
  • 9
  • 11
13

I had a similar problem on a fresh clone. I just forced the checkout with the --force (-f) flag

git checkout --force some_branch 

Probably not the best way of resolving the issue on a repo that you are making changes to, but in my case I was sure I hadn't made any changes, and just wanted to switch branch.

Adam Bowen
  • 346
  • 3
  • 9
11

Move the offending file to some other location. Then delete the offending file from your tree. Pull again. then diff the changes from your offending file over the file you just pulled. hack, but it works.

John Hammink
  • 111
  • 2
2

It seems that you have untracked files in your working copy, which are tracked on the other branch. Git refuses to checkout the other branch, since your currently untracked local files would be overwritten by the files on the other branch.

You can now

  • Add those files to your current branch, if those files are relevant for this branch
  • remove those files if they are not needed
Rudi
  • 19,366
  • 3
  • 55
  • 77
  • Another option would be to use `git stash` to save the files on the stash and restore them at sometime in the future (`git stash pop`) – Grizzly Jan 04 '12 at 02:47
  • The problem is that the files listed are tracked, and the current branch is reported as being up to date. The problem appears only when trying to checkout master. – Berislav Lopac Jan 04 '12 at 10:17
  • @BerislavLopac Do you have smudge/clean filters in your repo(http://progit.org/book/ch7-2.html)? Or an active crlf replacement setting (http://stackoverflow.com/a/2825829/311635)? – Rudi Jan 04 '12 at 11:07
  • @BerislavLopac At least to get a workable master back, you can also clone your repo into a new working copy (and optionally merge your current branch there). – Rudi Jan 04 '12 at 11:10
  • I had the same issue. My files were in gitignore and git was not allowing to checking out a previous target branch. I removed the file from the folder and it solved the problem. – John Kuriakose Aug 28 '18 at 17:47
1

Very similar to @JohnHammink answer but here goes.

Solution

  1. Remove file to another directory.
  2. Commit your changes (it might show you just removed two files).
  3. Add the file again to the directory.
  4. Commit (you should be able to see only one file being committed).

In my case this was because of a file that was renamed with only a change to the case. e.g. fooViewModel to FooViewModel. Git kept thinking that the same file was actually two separate files.

I think this has something to do with Unix being case sensitive while windows not really.

Hope this helps

YKa
  • 3,998
  • 4
  • 20
  • 31
0

I had the same problem, and this solved my issues (on Linux):

git config --global core.autocrlf input
jk_
  • 5,448
  • 5
  • 25
  • 23
0

As the message suggests, rerun the command w/ --force option.

flutter upgrade --force
Saif Hakeem
  • 875
  • 8
  • 9