Skip to content

Instantly share code, notes, and snippets.

@EHLOVader
Last active November 15, 2023 21:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EHLOVader/4d71826db94d56e64bce4785e4b68280 to your computer and use it in GitHub Desktop.
Save EHLOVader/4d71826db94d56e64bce4785e4b68280 to your computer and use it in GitHub Desktop.
Personal Oh Shit Gits

You accidentally committed a file you meant to ignore, and now you need to remove it but you don't want to lose your local copy.

Add ignores... gitignore.io maybe.

Remove whole folders

git rm --cached -r somedir

Remove a specific file

git rm --cached somefile.ext

You have accidentally used the wrong case for a file name and you are using a case insensitive file system so it won't detect the changes.

Use git mv to rename it to the right case

git mv -f yOuRfIlEnAmE yourfilename

Generally all of the files in the .gitignore are going to stay out of the repo, however if they get added explicitly they may keep coming up in commit logs. You may also begin to get new files in those same folders but you don't add them so the files start to rot.

The fix, remove files that are ignored explicitly. You can do this without losing your local files using the --cached command.

Remove all of the files that are listed in the .gitignore

git rm --cached `git ls-files -i -c --exclude-from=.gitignore`

There is a large chance that this will be too many files for the command prompt. In which case you may need to do them folder by folder

git rm -r --cached -- ignored_folder/ ignored_sub/folder ignored_file.md

So you are still using Master branches.

Here is how to fix it and switch that branch to a main branch

git checkout master

Rename branch

git branch -m main

Delete remote branch

git push origin --delete master

Push new branch

git push origin -u main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment