0

I am working on an Xcode project in macOS. The project is also under git version control. I have a branch in git that I created off of my local master branch. I have done some substantial work in it and have committed the changes. Issuing a git status command reports:

nothing to commit, working tree clean

I want to switch to the master branch and merge the changes from this branch into it. When I go to switch to the master branch I get a response like the following:

error: Your local changes to the following files would be overwritten by checkout:
    <List of files>
Please commit your changes or stash them before you switch branches.
Aborting

I am puzzled about why it says the files have changed. If I try to do a diff on one of the files, nothing happens; no output Is generated. In a certain sense, git seems to be treating at least some of these files as if they were new files even though they are not. I’ve tried adding the supposedly changed files to a git commit and git status still says

working tree clean

If I try to commit, git says

nothing to commit, working tree clean

If I try to do a git stash, it says

No local changes to save

What can be be done to fix this problem and allow me to merge the changes from this branch into the master branch?

SwissCodeMen
  • 4,222
  • 8
  • 24
  • 34
Tron Thomas
  • 871
  • 7
  • 20
  • 1
    Does this answer your question? [git checkout errors even though git status reports that working tree is clean](https://stackoverflow.com/questions/53214095/git-checkout-errors-even-though-git-status-reports-that-working-tree-is-clean) – evolutionxbox Sep 08 '20 at 19:25
  • Can you include all of the commands you are running so it's clear what you are doing – Nick Sep 08 '20 at 23:06
  • I tried applying both skip-worktree and assume-unchanged to all the files git was complaining about; git still complains the files have local changes that will be overwritten and will not allow the branch switch. – Tron Thomas Sep 09 '20 at 15:55

2 Answers2

0

This error happens when the branch (in your example master) 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

git fetch really only downloads new data from a remote repository - but it doesn't integrate any of this new data into your working files. Fetch is great for getting a fresh view on all the things that happened in a remote repository. Due to it's "harmless" nature, you can rest assured: fetch will never manipulate, destroy, or screw up anything. This means you can never fetch often enough.

Otherwise, your other option is to abandon your current branches changes with:

git checkout -f master
SwissCodeMen
  • 4,222
  • 8
  • 24
  • 34
  • I’m the only one working on this project, so it is hard to see how new changes could have happened on the master branch when I’ve been working on this other branch all this time. Anyway, I’ve already tried doing git fetch and that does not help. – Tron Thomas Sep 09 '20 at 15:24
0

I’m not sure what happened. As I mentioned in a comment to my question, I tried applying skip-worktree and assume-unchanged to all the files git was complaining had been changed and would be overwritten by the branch switch. I tried to do a branch switch and it failed like it always did.

After that I did another git status, and this time it showed there were two new files to commit. One of these files was among the group of files git had been complaining about with regard to the branch switch. The other file had never been mentioned before. A quite puzzling thing about this status report is that git listed both of these files as new, however they have both existed in the repository for quite some time.

I initially unstaged those files because I couldn’t understand why git thought they were new. A git status then showed them as untracked files. I staged them again, and performed a commit. This produced a spew of output that was really alarming. There were hundreds of lines that said something like:

delete mode 100644 <some file name>

Along with a few

create mode 100644 <some file name>

I was concerned that data had been deleted. I checked one of the deleted mode files, and it seemed to be fine

Anyway, after performing that commit, git status indicated there were no more pending changes, and at that point, I was actually able to perform the branch switch. So the problem appears to be solved.

Another confusing thing is that after switching branches, I then tried using git ls commands to checked if any of the files I had tweaked earlier still had skip-worktree and assume-unchanged applied to them, and no files were listed for either skip-worktree or assume-unchanged.

If anyone has any explanation for all of this, it would be good to hear about it.

Tron Thomas
  • 871
  • 7
  • 20