Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save hsn0x/c1bc8f1433fe7fb7f8251173aa9f78fc to your computer and use it in GitHub Desktop.
Save hsn0x/c1bc8f1433fe7fb7f8251173aa9f78fc to your computer and use it in GitHub Desktop.
Git basics: Help my case-sensitive filename changes don't commit

We have a file called timezone.js, and we commit this file to Git. All good and well.

File added to Git

But then we realized the whole repo used "time zone" with a space.

Apparently, there are three correct spellings of timezone: timezone, time zone, and time-zone.

With this in mind, we might want to uniform our file system and rename this file to timeZone.js. Let's go ahead and make that change.

File changed to git change

I renamed the filename in the above image but only changed the case sensitivity. Git doesn't pick this up.

So how can we commit this filename change?

Committing a single filename changepermalink

If it's just one file, running the following command is the easiest way to do this.

git mv timezone.js timeZone.js

This means move and can be used to move or rename a file.

Change detected in git

You can now commit and push this change, and it will reflect in Git as well.

Handling multiple case-sensitive file changespermalink

If you do changes on more than one file, you can use option one to do all of them by hand.

Or you can follow the following steps.

  1. Remove all of git cache
git rm -r --cached .

This command will remove the Git cached version of all files/folders in this directory. You will see all files in your git changes, but don't worry. The next step will fix it.

Remove git cache

  1. Re-add current status
git add --all .

This command re-adds all the files, making only the ones with changes appear.

Case sensitive file git commit

You can now go ahead and commit and push this change to reflect on Git.

Case sensitive file committed

So what about folders?permalink

I've added a folder called folder to my git repo.

Folder pushed to Git

And now let's rename it to Folder.

Again, this change is not picked up by Git as we saw with the file.

So let's try option one:

git mv folder Folder

This hits us with the following message:

fatal: renaming 'folder' failed: Invalid argument

This only happens on case insensitive systems like Mac.

As a fix for this option, we could run the following command.

git mv folder tmpFolder && mv tmpFolder Folder

This will work since we first rename it to something completely different. Then rename it back but with the correct case sensitivity.

But let's try option two to see what happens.

git rm -r --cached .
git add --all .

Capital folder

And it worked! So, the safe bet is always to use the remove cache function.

There are some other ways of doing this as well. What is your preferred way of renaming a case-sensitive file/folder in Git?

You can find my test on the following GitHub repo.

Thank you for reading, and let's connectpermalink

Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter

source

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment