Create Global Gitignore and Safely Clear Staged Files

Last modified: 
Thursday, April 9th, 2015
Topics: 
Git

The following describes in some detail how to set a global .gitignore file and safely clear out any previously staged files which should be ignored from here on.

In this example, we're going to presume we have a PhpStorm project under version control at docroot. We want to hide the IDE's configuration files, which are stored in docroot/.idea. We thought of this a little late, so our git status returns something like this:

$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD file..." to unstage)
#
#   new file:   .idea/deployment.xml
#   new file:   .idea/docroot.iml
#   new file:   .idea/encodings.xml
#   new file:   .idea/modules.xml
#   new file:   .idea/scopes/scope_settings.xml
#   new file:   .idea/vcs.xml
#
# Untracked files:
#   (use "git add file..." to include in what will be committed)
#
#   .idea/.name
#   .idea/workspace.xml

We're downstream, so we don't want to add any ignore rules to the upstream maintainers' project. This puts the docroot/.gitignore file off limits for editing. To work around this, we set a config option to create a global .gitignore file in our user directory.

git config --global core.excludesfile ~/.gitignore

I next create the file and add the following rules to it. You don't have to use vim for this, obviously. I'm just used to vim, so that's what I used.

$ vim ~/.gitignore

# Always and everywhere ignore PhpStorm's .idea directories.
**/.idea

:wq

Running git status now returns the following:

$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD file..." to unstage)
#
#   new file:   docroot/.idea/deployment.xml
#   new file:   docroot/.idea/docroot.iml
#   new file:   docroot/.idea/encodings.xml
#   new file:   docroot/.idea/modules.xml
#   new file:   docroot/.idea/scopes/scope_settings.xml
#   new file:   docroot/.idea/vcs.xml
#

Our untracked files are now being ignored, but we still have a collection new files staged that we don't want to commit. This is because the files in question where already being tracked before we added our ignore rule. To rectify this we run git rm with the --cached flag on docroot/.idea.

$ git rm -r --cached docroot/.idea/
rm 'docroot/.idea/deployment.xml'
rm 'docroot/.idea/docroot.iml'
rm 'docroot/.idea/encodings.xml'
rm 'docroot/.idea/modules.xml'
rm 'docroot/.idea/scopes/scope_settings.xml'
rm 'docroot/.idea/vcs.xml'

Checking our status once more, we can see the previously staged .idea files have been unstaged and are now being ignored.

vagrant@precise64:/var/www/html/Publisher7_sprout$ git status
# On branch master
nothing to commit (working directory clean)

Meanwhile, an ls in docroot/.idea confirms the files are still intact and have only been rm from the git index.

docroot/.idea$ ls -R1
.:
deployment.xml
docroot.iml
encodings.xml
modules.xml
scopes
vcs.xml
workspace.xml

./scopes:
scope_settings.xml


The operator of this site makes no claims, promises, or guarantees of the accuracy, completeness, originality, uniqueness, or even general adequacy of the contents herein and expressly disclaims liability for errors and omissions in the contents of this website.