How to Stop Tracking and Start Ignoring in Git
In Git, the ignored files are tracked in a file, called .gitignore. You have an opportunity to add file paths and directory paths that you don’t wish to be tracked by Git in this file. Almost all programmers, working with Git, are eager to learn how to stop tracking files, which were already inserted into a git repository. This snippet is aimed at explaining the process of stopping to track and ignoring changes to a file in Git. The .gitignore will prevent untracked files from being added to the set of files tracked by Git, however, Git will continue to track the files that are already being tracked even when they are added to .gitignore.
Ignoring changes to a file¶
If you have a tracked file and want to ignore it, you need to add the path of that file or directory into the .gitignore file. Afterwards, you need to tell Git not to track it. For that purpose, you should run the command below:
git rm --cached <file-path>
If you want to do that for the folders instead of files, you should add the -r option to the command above like so:
git rm -r --cached <folder-path>
Suppose, you have a folder containing a lot of files. The following command instructs Git to stop checking that folder every time for changes, as it won't have any. The --assume-unchanged index will be reset, and files will be overwritten if there are upstream changes to the file or folder when you pull. As the idea is ignoring the local changes, you can delete all of them by running the git reset --hard command.
git update-index --assume-unchanged <file-path>
For getting a list of directories or files that are --assume-unchanged, you should run the command below:
git ls-files -v|grep '^h'
The following command will instruct Git that you need your own independent version of the file or folder. After that, any changes made to that file(s) will not be flagged as a change in Git.
git update-index --skip-worktree <file-path>
What is Git Repository¶
Git repository serves as special storage for the files of your project. With git repository, it is possible to save the versions of your code, as well as, to have access to them. If you want to create a new git repository, you need to run the git init command. It is used once to initialize a new repository. Another one-time operation is the git clone command, with the help of which you can create a clone of an existing repository.
What is the git rm Command¶
The git rm command can remove particular files or a group of files from a git repository. You can also use it to remove files from the working directory and the staging index. But it is not possible to remove a file only from the working directory. The git rm command doesn’t remove branches either.
What is a .gitignore File¶
Generally, Git classifies the files of the working copy as 3 things:
- A tracked file - the one that has been committed or staged before;
- An untracked file- the one that has not been committed or staged before;
- An ignored file- the one that has been commanded to be ignored.
In your working copy, the files are visible for git as untracked, tracked or ignored. Ignored files are those you told Git to ignore. In case you want to commit this kind of files, you should derive them from the repository source. The ignored files are stored in the file called .gitignore. There is no git ignore command, hence you should edit and commit these files by hand.