0

I was following a guide to ensure your JavaScript and CSS files were always refreshed when you updated them by adding ?v= to the filename. However Windows and git do not like this naming convention but I did manage to name the file audit.css to audit.css?v=1.0 using Cygwin. However as git doesn't like this filename it is stuck in changes to be committed. How can I removed the deleted change for this file? I have tried a few changes from other pages on stackoverflow but they have failed.

Cygwin$ git status
On branch main
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        renamed:    audit.css?v=1.0 -> audit.css

Cygwin$ git restore --staged audit.css
Cygwin$ git status
On branch main
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        deleted:    audit.css?v=1.0

Cygwin$ git restore --staged audit.css?v=1.0
error: invalid path 'audit.css?v=1.0'

Cygwin$ git reset 'audit.css?v=1.0'
error: invalid path 'audit.css?v=1.0'
fatal: make_cache_entry failed for path 'audit.css?v=1.0'
CraigW
  • 19
  • 4

4 Answers4

2

Since you didnt commit anything to remote branch (ie github), you can just roll back your changes like this

git reset --hard HEAD

what that will do is remove all your changes to the last time you pushed up into your remote repo (ie your last git push)


The other way is you could just change your file through file explorer. If you open your files and see it, right click, rename to new name. However this might not work because you changed the file extension previously to '.css?v=1.0' and that might have corrupted your file.

  • Rule of thumb, extensions are a way computers know what type of file it is and you shouldnt change anything after the '.'
  • Another rule is dont name files with special characters or spaces. It causes a lot of problems with paths and compatibility issues across apps

The other other way is just delete the file directly and start that file from scratch again. you can remove the file with file explorer or use this command in command line

go to the location of the file

rm audit.css?v=1.0

Raie
  • 61
  • 6
  • Thanks for the suggestions. I have been able to delete the file audit.css?v=1.0 so all that exists now is the audit.css. However it seems that git still retains memory of the audit.css?v=1.0 file and is reluctant to let go. – CraigW Jan 24 '23 at 07:39
  • I am guessing 'git still retains memory' means you had it previously added to a commit (you did git add audit.css?v=1.0). A (git checkout -- audit.css?v=1.0) should work to remove from staged. If that doesnt work, You could just do start the repo in a new location by pulling it in from cloud (ie, go to a completely new location such as Documents/anotherFolder, and do a git clone repoName). That will not have any memory of old staged commits – Raie Jan 24 '23 at 17:00
  • Your suggestion fixed the issue. I performed a git checkout then was able to perform a git commit. – CraigW Jan 25 '23 at 14:19
1

The Windows OS is incompatible with that file naming convention.

Windows filenames cannot contain " * / : < > ? \ |. See this question.

  1. Easier: Move git operations into the Windows Subsystem for Linux (WSL). You can install it thru the Microsoft Store, IIRC it's just called "Ubuntu".

WSL doesn't have the same filename restrictions that Windows does. WSL accomplishes this by recording the Linux filename elsewhere, allowing Linux to "think" it has gotten away with adding Windows-restricted characters to filenames.

  1. Harder: Install Docker Desktop for Windows and use that for git operations.

This might not work since if the software reading/writing to the files still probably needs to run on Windows (such as an IDE), the same filename restrictions apply.

  1. Do away with the ?v= filename suffixes.

This smells like it's probably a misapplication of version control. The ?v=... convention to me looks like a string you'd put in your HTML to defeat the cache via URL parameters. It would probably be better to have the HTML contain the ?v=... strings, and let them be ignored by the web server.

amphetamachine
  • 27,620
  • 12
  • 60
  • 72
  • Thanks for the help. I had misunderstood the cache defeating guide for JavaScript and CSS files. I should have realised something was wrong when Windows objected to the ? in a file name. – CraigW Jan 24 '23 at 07:33
1

I don't have a test system, but you can be more heavy-handed with your removal of local changes

BEWARE both of these will throw away state, so take caution with any other parallel changes you'd like to keep

Discard local changes

git checkout -- .  # <-- note .

Discard any changes to HEAD

git reset --hard
ti7
  • 16,375
  • 6
  • 40
  • 68
  • Thanks for these commands. I do have changes to other files that I haven't committed that I would like to keep - git checkout looks like it will discard those changes. I guess I should have committed the changes before I attempt to make any changes I am unfamiliar like git mv. – CraigW Jan 24 '23 at 07:36
  • possibly - this is the first mention of `git mv` though! can you undo the change with it? – ti7 Jan 24 '23 at 15:23
1

You misunderstood the guide.

It's telling you to add that to your url, at the end of the filename. The filename should still be audit.css.

In all text processing there's a little dance going on. It's been carefully orchestrated and tweaked over ... wow, over literal generations now, to be as unintrusive as possible, but you've just tripped over one of the entries to the rabbit warren.

In case you're feeling motivated, here's the start of the particular rabbit hole you fell in to. Search for a question mark or just start reading. That document tells you how your url is parsed and some of what's supposed to be done with the pieces.

The referenced file is still intended to be audit.css; web caching between your server and whatever browser is supposed to remember the entire cached url but your server's supposed to interpret the ? and what follows as a query with parameters, which can sometimes be and here is supposed to be ignored. So if you change the ?v= query the caches will see it doesn't match what they've got cached and pass the request along to your server, which if you want to keep your sanity in this use you've set up to ignore that particular query.

jthill
  • 55,082
  • 5
  • 77
  • 137
  • Thanks for the explanation. I had made the incorrect assumption that the file on the disk had to have the same name as the file in the html. However you are saying that adding the ?v to the end of the URL tells the browser to always download a new copy of the file from the server i.e. don't use the cached file. – CraigW Jan 24 '23 at 07:31
  • It's not exactly like that. I did say rabbit hole. Adding the query lets you change the url without changing the file name. *Changing* the url makes intermediate caching not recognize that they have content cached. If they're caching, once they see what gets delivered for the new url they'll save it and serve it up again until you change the url again. – jthill Jan 24 '23 at 08:05