Stop tracking and start ignoring
Tags: git, untrackfiles, removefiles, removefolders
Published: 2019-02-17 08:45:16

---
title: Stop tracking and start ignoring
published: true
tags: git,untrack files,remove files,remove folders
canonical_url: https://svijaykoushik.github.io/blog/2019/02/17/start-ignoring/
cover_image: https://svijaykoushik.github.io/blog/img/2019_02_16/post_header.jpg
description: "Here I share a tip to delete files unwanted & unrelated to the project from a git repository"
---

Hello world,

I’ve been playing around with GitHub & Git over the past few months and most of the time I accidentally commit and push files that are totally unrelated to the project. Usually they are the environment files like _node\_modules_ & _build_ directories and _package.json_ when using node.js as I use Gulp to minify css & JS and files like _\_site_ folder and _Gemfile.lock_ when I use Jekyll for my blog.

The reason for this problem? I forget to add the _.gitignore_ file to my repository. The _.gitignore_ file contains all the ignore rules (usually names of files and folders to be ignored) for the project. But there is a catch. The _.gitignore_ file will tell Git to ignore the existence of only those files that haven’t been pushed. Since I have already pushed the files, adding them to the ignore rule won't work. Git will keep tracking the changes to those files and nag me to commit those changes.

I could delete these files and push the deletions to the repo but, they’re going to be recreated every time I build the project. So, that doesn’t work. A quick search on the internet gave me the exact solution I needed. There is a way to remove these files from the repo and keep them locally. By executing the following command in a CLI like **git bash** or **cmd** I deleted the files from the repo without removing them from my local file system.

```

    git rm --cached <file name>
    E.g. git rm --cached package.json
    
```
  
 ![Break down of remove a file only from repository command in git](https://svijaykoushik.github.io/blog/img/2019_02_16/rm_cached_file.jpg)_A break down of the command to remove a file only from the repository_  

```

    git rm –r --cached <directory name>
    E.g. git rm –r --cached _sites
    
```
  
 ![Break down of remove a folder and it's contents only from repository command in git](https://svijaykoushik.github.io/blog/img/2019_02_16/rm_cached_folder.jpg)_A break down of the command to remove a folder and it's contents only from the repository_  
The `--cached` flag removes the files from the repository and leaves the local copies undisturbed. And the `–r` flag recursively removes the files inside the directory specified.

Now that I removed the files from the repo, Git thinks that the local copies of the deleted files are something new I added to the repo. So adding these file names to the _.gitignore_ file will tell git to ignore these files and they won’t be pushed again.

Since this accident happens to me frequently, I decided to post this here so I wouldn't forget to add the ignore rules to the repo. Even if I do forget, I know where to look for the solution.

To make things more simple I found a site [gitignore.io](https://gitignore.io) that generates _.gitignore_ file based on the Operating systems, IDEs and programming languages that I provide in an all in one text box. ![Screenshot of the site gitignore.io](https://svijaykoushik.github.io/blog/img/2019_02_16/gitignoredoti_shot.jpg)_A screenshot of gitignore.io's homepage_