How to Stop Tracking and Start Ignoring in Git
This snippet explains how to stop tracking and ignore changes to a file in git.
In Git, ignored files are tracked in a file called .gitignore. You can add file and directory paths that you don’t want Git to track in this file. Many developers need to stop tracking files that were already added to a git repository. This snippet explains how to stop tracking and ignore changes to a file in Git. The .gitignore file prevents untracked files from being added to the repository, but Git will continue to track files that are already being tracked even after they are added to .gitignore.
Ignoring changes to a file
If you have a tracked file and want to ignore it, add its path to the .gitignore file. Then, tell Git to stop tracking it by running the command below:
Remove file from Git tracking
git rm --cached <file-path>To do this for folders instead of files, add the <kbd class="highlighted">-r</kbd> option to the command above:
Remove folder from Git tracking
git rm -r --cached <folder-path>The commands above keep the local files or directories intact. However, if someone else pulls these changes, the files will be deleted from their working directory.
Suppose you have a folder with many files and want Git to skip checking it for changes to improve performance. The <kbd class="highlighted">--assume-unchanged</kbd> flag tells Git to assume the file hasn't changed. Note that this flag is primarily for performance optimization and does not prevent upstream changes from overwriting your local file during a pull. To revert this flag, run:
Set assume-unchanged
git update-index --assume-unchanged <file-path>To get a list of files marked as <kbd class="highlighted">--assume-unchanged</kbd>, run:
List assume-unchanged files
git ls-files -v | grep '^h'Revert assume-unchanged
git update-index --no-assume-unchanged <file-path>The <kbd class="highlighted">--skip-worktree</kbd> flag tells Git to keep your local version of a file or folder and ignore any changes made to it in the working directory.
Set skip-worktree
git update-index --skip-worktree <file-path>Revert skip-worktree
git update-index --no-skip-worktree <file-path>What is Git Repository
A Git repository stores your project files and version history. Run git init to initialize a new repository, or git clone to copy an existing one.
What is the git rm Command
The git rm command removes files from the staging index and optionally from the working directory. It does not remove branches.
What is a .gitignore File
Git classifies files in your working copy as tracked, untracked, or ignored. Ignored files are listed in the .gitignore file. Since there is no git ignore command, you must edit and commit this file manually.