Stopping tracking a file in Git

To untrack a file that has been committed, what you do is adding a new ignoring rule and removing the file in a commit.

Below is a full example of ignoring /assets/my-style.css .

Step 1: add below content to .gitignore file to add an ignore rule.

/assets/my-style.css

Note: See ignoring files for how to create your  .gitignore file and write ignore rules.

Step 2: remove it from repository and commit the changes .

# Stage .gitignore
$ git add .gitignore

# Remove my-style.css file from staging area, my-style.css
# in working directory is left alone.
# --cached, only apply to staging area
$ git rm --cached -- assets/css/my-style.css

# Commit them
$ git commit

Thus, my-style.css will not be tracked any more. Be aware that it still remains in previous history.

To stop tracking a directory is similar, just replace above file with a folder in .gitignore file and git rm command.

Leave a Reply