4

I'm having an issue where I'm trying to checkout a branch, but I can't because:

Your local changes to the following files would be overwritten by checkout: lib/file.php Please commit your changes or stash them before you can switch branches. Aborting

Ok, so I run git status, and I get:

On branch current-branch nothing to commit, working directory clean

I needed to edit .git/info/exclude to exclude lib/file.php because that file was causing problems on my specific machine. But when I navigate to exclude, I don't see the file listed anymore. I checked all three branches that are currently active but I don't see lib/file.php listed in my exclude file.

What is going on here?

Not a duplicate - answer/link suggested the author never actually commited files. I understand that concept. This is a different issue.

kawnah
  • 3,204
  • 8
  • 53
  • 103
  • Are you running these commands in the same directory? – Joe Phillips Nov 30 '17 at 15:18
  • Was `lib/file.php` ever part of your repo? If so, then you may need to read this: https://stackoverflow.com/questions/1274057/how-to-make-git-forget-about-a-file-that-was-tracked-but-is-now-in-gitignore – JDB Nov 30 '17 at 15:21
  • Yes, all in project root – kawnah Nov 30 '17 at 15:21
  • @JDB yes it was – kawnah Nov 30 '17 at 15:21
  • Ok... then git is in a weird state, because your ignore file is preventing git status from "seeing" the changes, but checkout still "sees" that the file in the repo will overwrite the local file. Check out that link. Should fix things for you. – JDB Nov 30 '17 at 15:22
  • seems your branch doesnt know the remote branch. have you tried this, `git branch --set-upstream-to origin/master` – danglingpointer Nov 30 '17 at 15:24
  • @LethalProgrammer - That is incorrect. The error messages show that the local branch is tracking the remote correctly. – JDB Nov 30 '17 at 15:25
  • @JDK ok then its some XY problem here. – danglingpointer Nov 30 '17 at 15:26
  • @kawnah, add the `git reflog` output to your question then its easy to follow what commands you executed. – danglingpointer Nov 30 '17 at 15:28
  • Possible duplicate of [How do I resolve git saying "Commit your changes or stash them before you can merge"?](https://stackoverflow.com/questions/15745045/how-do-i-resolve-git-saying-commit-your-changes-or-stash-them-before-you-can-me) – tkruse Nov 30 '17 at 15:35
  • @tkruse nope not a duplicate. Answer in link suggests in first step to commit files. I have already done this, thus getting `nothing to commit` – kawnah Nov 30 '17 at 15:36

2 Answers2

5

Currently you are on a branch where the file lib/file.php is not in git, but in your folder. The branch you want to switch to has the same file in git. If git switched to that branch, the current file you have right now would be lost forever.

The git error message is a bit confusing. It should tell you to either delete lib/file.php before switching to that branch, or 1. add the file, 2. commit it 3. switch to other branch.

tkruse
  • 10,222
  • 7
  • 53
  • 80
  • Ok I think I get it now. – kawnah Nov 30 '17 at 15:37
  • But I try to add and nothing adds, because it's already being watched by git? – kawnah Nov 30 '17 at 16:01
  • Deleting isn't an option here, because I don't want that delete pushed to remote. – kawnah Nov 30 '17 at 16:01
  • Files may be watched in one branch, but not watched in another branch. It seems it is not watched in your current branch, but watched in the branch you want to switch to. Adding should work, but if the file is being ignored you might have to force adding. deleting just means deleting in the file system, not using git rm. – tkruse Nov 30 '17 at 16:55
0

On branch current-branch nothing to commit, working directory clean

This is because changes made to this .git/info/exclude cannot be committed as it is your specific setting. So it says nothing to commit. But if you modify .gitignore then it is applicable to anyone who uses this repo.

Please read more about .git/info/exclude

Naveen KH
  • 153
  • 2
  • 11
  • I needed to do that and not .gitignore because the file in question broke my build only and nobody elses. so if I used .gitignore it would be ignored in everyones version, so if a commit was made on it by someone else, it would've caused conflict. I'm the only one that needs to have it ignored. It's a messy situation and not ideal, I know, but it is what it is. Thanks for your answer – kawnah Nov 30 '17 at 15:45
  • but what's more confusing now is .git/info/exclude doesn't have that file listed. So why would it behave this way? – kawnah Nov 30 '17 at 15:51