Cleaning up git github repository without deleting .git directory

Image for post
Image for post

This article contains a general solution for the following issues. Why I insist on not delete the .git directory for the following kind of purposes? answer: read the Warning! section at the end of this article.

Issues:

  1. Removing files from git repository without deleting it from the local repository (disk)
  2. Applying .gitignore to committed files in git Github
  3. Reducing git repository size
  4. Clearing Github history
  5. Making the current commit the only (initial) commit in a git GitHub repository
  6. Deleting all commit history in Github
  7. You have already added/committed some files to your git repository and you then add some of them to your .gitignore. But these files will still be present in your repository index and want to get rid of them.
  8. Ignoring files that have already been committed to the repo.
  9. Removing files from git after adding/updating .gitignore
  10. Ignoring a directory from a git repo after it’s been added.
  11. git rm a file without deleting it from disk
  12. Removing all git histories

Attention:

  1. Make sure your .gitignore file is up-to-date and contains all the correct patterns you want to ignore.
  2. Use gitignore.io service to create useful .gitignore files for your project

Solutions:

1- Solution for both Remote repository and Local :

git checkout --orphan newBranch
git add -A # Add all files and commit them
git commit -am "Clean Repo"
git branch -D master # Deletes the master branch
git branch -m master # Rename the current branch to master
git push -f origin master # Force push master branch to github

That’s it! It’s done and you have a clean history and repository!

Optional:

At the end to cleanup unnecessary files and optimize the local repository do this:

git gc --aggressive --prune=all     # remove the old files

Read more about: git gc command here

2- Solution for local :

Step 1:

Commit all your changes, including your .gitignore file.

git add .
git commit -m “messages”

Step 2:

Remove everything from the repository or un-track all files in your git repository.

$ git rm -r --cached . 
  • rm is for remove
  • -r allow recursive removal
  • - -cached will only remove files from the index. Your files will still be in disk
  • The . indicates that all files in the current directory will be untracked.
  • If you want un-track a specific single file do this:
git rm --cached <file>
example:
git rm --cached foo.txt

If you want un-track a single directory do this:

git rm --cached -r <directory>
example:
git rm --cached -r MyFolderName

Step 3:

Re add everything except those that match rules in your .gitignore and then commit them.

$ git add . 
$ git commit -m "Clean up ignored files"

Then your repository is clean!

Warning!

rm -rf .git

And want to reconstruct the git repo with only the current content and finally do:

git push -u --force origin master

This might bring some problems! It also removes the configuration of the git repository (.git/config). Deleting .git/ always causes huge issues when you have git submodules.

Written by

Web geek, Self-taught full-stack web developer, Learning Python, Laravel, Vuejs, UX/UI design, Nuclear Physicist PhD

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store