A file seems to still be tracked after running `git update-index --assume-unchanged <file>`

191 views Asked by At

I ran the command git update-index --assume-unchanged .\src\main\resources\application.properties in my local directory, and the application.properties file disappeared from the Changes section of Source Control in VS Code, as I expected.

However, the diff still shows red deletions & green additions, as if the file is still being tracked. <-- this is shown in the screenshot below

I'm also prevent from switching branches, for the reason that "changes would overwrite that file"

Any ideas why this is happening? I want a base version in my remote repository that I can change locally without the changes being tracked.

I saw the --skip-worktree option and tried git update-index --skip-worktree .\src\main\resources\application.properties, but got the same results.

Here's the git commands & output

PS C:\r\Body-Tracking-Uploader> git update-index --assume-unchanged .\src\main\resources\application.properties
PS C:\r\Body-Tracking-Uploader> git status
On branch upload-exerciseInfo-aws
Your branch is up to date with 'origin/upload-exerciseInfo-aws'.

nothing to commit, working tree clean
PS C:\r\Body-Tracking-Uploader> git branch
  main
* upload-exerciseInfo-aws
PS C:\r\Body-Tracking-Uploader> git checkout main
error: Your local changes to the following files would be overwritten by checkout:
        src/main/resources/application.properties
Please commit your changes or stash them before you switch branches.
Aborting
PS C:\r\Body-Tracking-Uploader>

Screenshot of the Source Control tab/view in VS Code

2

There are 2 answers

0
Dan Bitter On

I'm not totally sure what the problem was, but I took the following steps

  1. Copied the contents of application.properties to a separate location
  2. Reverted application.properties to match the remote version
  3. Committed/pushed all code changes related to my progress so far
  4. Merged this feature branch into main (it's not feature-complete yet, but functions in its current state)
  5. Created a new feature branch off main in the remote repository (to continue the previous WIP feature)
  6. Pulled the latest main into my local
  7. Ran git update-index --skip-worktree .\src\main\resources\application.properties
  8. Checked out the newly-created feature branch
  9. Restored my local changes to the application.properties file

Now the changes do not show in the Source Control view, and they don't prevent me from moving between branches locally.

0
bk2204 On

You're looking for a way to ignore changes to tracked files, and the reason neither of the options you've tried work is because Git doesn't support that, as outlined in the Git FAQ:

Git doesn’t provide a way to do this. The reason is that if Git needs to overwrite this file, such as during a checkout, it doesn’t know whether the changes to the file are precious and should be kept, or whether they are irrelevant and can safely be destroyed. Therefore, it has to take the safe route and always preserve them.

It’s tempting to try to use certain features of git update-index, namely the assume-unchanged and skip-worktree bits, but these don’t work properly for this purpose and shouldn’t be used this way.

If you're looking for a way to deal with configuration in config files, which is what it looks like you're doing, then the FAQ outlines how to do that:

If your goal is to modify a configuration file, it can often be helpful to have a file checked into the repository which is a template or set of defaults which can then be copied alongside and modified as appropriate. This second, modified file is usually ignored to prevent accidentally committing it.