gitgithub

When running: git update-index --assume-unchanged <file>, file is assumed un changed but when changing branch, error appears


I have a file in my Pycharm which I have changed so the commits are shown. As soon as I run the following command thorough the CLI and try to change the active branch (via CLI or Pycharm GUI):

git update-index --assume-unchanged <file>

I encounter the following error:

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

although I have used the --assume-unchanged option, it says that there are changes but my working tree is clean.

Additionally, I have tried restarting Pycharm and also my computer, however the issue still persists. How can I fix this?


Solution

  • This issue will persist if the file f you are trying to "untrack" in your main branch, is/will cause conflicts in the other branch, in which you are attempting to checkout. So for example, you perform some changes in f, and do not want to track them, you performed a

    git update-index --assume-unchanged f
    

    Now, if you do a

    git checkout other_branch
    

    It will raise the error which you quote. This happens because --assume-unchanged is actually there to improve speeds of the git-relates processes. For files which are extremely big and may contain numerous changes, and the author feels it is better to ignore the check for changes made on this file, one should use this option.

    However, for your use case, the --skip-worktree option would be correct, wherein you want to change the file, but want to ignore its changes so as to perform checkout, push, pull, merge, etc. More about this here.

    Now, if you want to play it completely safe (or rather proceed primitivly), and simply want to carry forward the changes made in file f to another branch, you can make the changes and checkout to a new branch via:

    git checkout -b new_branch
    

    This way your changes in f are carried forward in the new branch. But if you still want to checkout to a branch with a deviated history from your current branch, and carry forward the changes and want to keep it simple, then simply copy them manually. However otherwise, --skip-wortree would be a nice option.

    Hope this helps!