Git ignoring and removing files

Updated on 28 Dec 2018

.gitignore - specific file types

Ignore all *.xlsx files in the data directory.

data/*.xlsx

Before .gitignore

After .gitignore

.gitignore - entire folders

Ignore an entire folder and its contents, also includes sub-folders.

data/**

Removing files from the commit

You might have made a mistake and committed a file before adding it to the .gitignore file. Adding a rule to the .gitignore file doesn’t automatically remove it from the commit. To do that, we have to do this.

git rm --cached data/test8.xlsx

You can also remove a whole heap of files with -r.

git rm -r --cached data/*.xlsx

Using text patterns to remove files

Removing a file after push

The easiest way to remove a file from GitHub after a push and after you’ve done your .gitignore is to simply delete it via the GitHub UI. However this can be tedious especially if you have many files or you want to remove them from all your commits.

Another technique is with the use of git filter-branch. This will remove the entries from GitHub and the histories as well. WARNING this will also remove the file from your file system as well!

git filter-branch -f --index-filter 'git rm --cached --ignore-unmatch data/test8.xlsx' --prune-empty --tag-name-filter cat -- --all
git push origin --force --all
  • The --force is force. You are need to force the changes to go in because they are part of an unrelated history. It is possible on another machine that attempting to do a pull will result in errors.

  • The --all means that we are pushing the changes on ALL branches.

  • --prune-empty means to remove commits where files were not touched. Some git tasks will create an empty commit when they are executed; I.e. a commit where there were no changes.

  • --tag-name-filter is the filter for rewriting tags. It has the syntax of --tag-name-filter cat to update the tags.

  • -- separates the options for filter-branch and rev-list. Ref’s revision is the last parameter to be supplied, and you separate the options with --. Using --all seems to be standard.

  • --index-filter this is the filter for rewriting the index.

If that is the case, we can use the --allow-unrelated-histories flag to force the changes.

git pull --allow-unrelated-histories