DEV Community

Usman Zaheer
Usman Zaheer

Posted on

Git: Ignore files locally

TL;DR

  1. Navigate to .git/info, in the project folder
  2. Open the exclude file in your editor
  3. Put in the files you want to ignore locally (path must be relative to root of project)
  4. Run the following command in the terminal (in your project folder) : git update-index --skip-worktree <path-names>

All projects have some configurations that are meant to be private. When you’re working on a project in a private repository, there may be many configurations specific to the production or staging environment for-example, the database connection information.

But these relate to the environment on the server. What if you want to change the configurations for local development? You can’t push these local changes, and putting these configuration files in the .gitignore file is also a no-go as these files need to be tracked by git.

Turns out you can ignore local files using the power of Git! If you navigate to the .git folder, there is a folder in there called info, which should have one file inside called exclude. Edit this file and add all the local files you want to locally ignore (this file follows the structure of the root .gitignore file, so the path of files should be relative to the root project folder as is the case with the .gitignore file).

This will ignore any new files you want to ignore, but what about the files already being tracked by git (like the db connection information) ? To do that we’ll need to update the git cache to ignore these files. To do this, we can run the command: git update-index --skip-worktree <path-names>.

To bring these files back to being tracked by git (in the case that you need updated configurations from the repository), simply run git update-index --no-skip-worktree <path-names>. For more information about the update-index command, follow this link.

Top comments (3)

Collapse
 
shadowbq profile image
shadowbq

I use skip, and create these git aliases in my .gitconfig

skipped = "!f(){ git ls-files -v ${1} | grep ^S;};f"
skip = update-index --skip-worktree --
unskip = update-index --no-skip-worktree --
Enter fullscreen mode Exit fullscreen mode
Collapse
 
jonatasoc profile image
Jonatas de Oliveira Coêlho

Thank you!

Collapse
 
clutchdame profile image
ClutchDame

Thanks a lot!
stash and stash pop were kiling me :D