Stop Tracking and Start Ignoring in Git

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.

The files that were ignored are tracked in a file, are named .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 command will untrack but keep your file uninjured at the same time. It is especially good for autogenerated files. Anytime proceeding a git commit you are not forced to commit a copy of those files.

We can distinguish 3 main options.

For keeping the local file for you, but deleting it for everyone, when they try pulling, use the following command:

git rm --cached <file-name>   
or
git rm -r --cached <folder-name>

You can also act like this:

git rm -r -- cached.
git add

For preventing Git from detecting changes in the files, you can use the following command, as well:

git update-index --assume-unchanged [path]

In case you wish to inform Git that you want your independent form of the files, not wishing to overwrite the production/config files, act as follows:

git update-index --skip-worktree <path-name>

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

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.


Do you find this helpful?

Related articles