Sujay
Sujay

Reputation: 2196

Git checkout generates an error on --assume-unchanged files

After I run git update-index --assume-unchanged path/to/file on a particular branch, I'm unable to switch branches using git checkout.

It throws the following error:

error: Your local changes to the following files would be overwritten by checkout:

      path/to/file

Please, commit your changes or stash them before you can switch branches.

Aborting

But both git diff & git status tells me there is no difference & there is nothing to commit/stash.

How do I switch out of the branch?

Is there a better alternative to git update-index --assume-unchanged (other than .gitignore, because I don't want it to be ignored)? (Same as question 2 @ git update-index --no-assume-unchanged doesn't work)

Upvotes: 29

Views: 6906

Answers (2)

Matthew Clark
Matthew Clark

Reputation: 1965

I ran into the same thing today, and most of the answers I found, disappointingly, were similar to @Sujay's -- they didn't actually say how to get around the git checkout error.

Well, I found a solution that worked from this page (couldn't figure out who the author was so that I could give credit):

The solution that worked for me was to use --skip-worktree. However, like some above, I struggled with being able to switch between a ticketed branch and the main branch without git complaining even after I had set the --skip-worktree flag on the file whose changes I wanted to remain local.

...

  1. cp <local-only_file> ~/
    • copy file that has your local-only changes to somewhere safe on the filesystem
  2. git checkout <local-only_file>
    • in working tree, checkout file so that it matches master branch file
  3. git update-index --skip-worktree -- <local-only_file>
  4. cp ~/<local-only_file> ./
    • copy file in question from safe location back into working tree
  5. git diff
    • no changes should be shown; if you push to the main repo, no changes in <local-only_file> are included in the push

The full solution began with git update-index --no-assume-unchanged <filename> before proceeding with the steps above. After following these steps, I was able to switch branches.

Upvotes: 3

Sujay
Sujay

Reputation: 2196

After some amount of use I now prefer git update-index --skip-worktree which is a much better alternative to the git update-index --assume-unchanged.

Check out Difference Between 'assume-unchanged' and 'skip-worktree' for further information.

I also am planning to check out the method specified in this answer to git assume unchanged vs skip worktree - ignoring a symbolic link, which seems to be more defensive approach.

Upvotes: 4

Related Questions