Cleaning up git github repository without deleting .git directory

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. Un-track files already added to git repository based on the new updated .gitignore
  2. Removing files from git repository without deleting it from the local repository (disk)
  3. Applying .gitignore to committed files in git Github
  4. Reducing git repository size
  5. Clearing Github history
  6. Making the current commit the only (initial) commit in a git GitHub repository
  7. Deleting all commit history in Github
  8. 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.
  9. Ignoring files that have already been committed to the repo.
  10. Removing files from git after adding/updating .gitignore
  11. Ignoring a directory from a git repo after it?s been added.
  12. git rm a file without deleting it from disk
  13. Removing all git histories

Attention:

Before proceeding anything:

  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 :

Attention! This solution will not keep your old commit history around since it will delete the master branch! and delivers you a new master branch

git checkout –orphan newBranchgit add -A # Add all files and commit themgit commit -am “Clean Repo”git branch -D master # Deletes the master branchgit branch -m master # Rename the current branch to mastergit 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 :

If you don?t have a remote repository and all are in local (disk) you can simply.

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!

If you follow the brute-force approach:

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.

18