Git checkout generates an error on --assume-unchanged files
Asked Answered
V

2

29

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)

Vergos answered 24/9, 2012 at 13:46 Comment(0)
V
4

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.

Vergos answered 16/1, 2013 at 12:48 Comment(2)
Hi @Sujay, this still, however, doesn't allow you to checkout to another branch. Did you find a solution to branch-switching after skip-worktree or assume-unchanged?Herrera
Sorry @RutoCollins I wasn't able to figure that one out. I generally prefer stashing and unstashing now to get out of that.Vergos
P
3

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.

Plumbic answered 4/11, 2021 at 17:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.