To untrack files already added to a Git repository, you can follow these steps:

  1. Step 1: Commit any outstanding changes: Before proceeding, make sure you have committed any changes you want to keep. This is to avoid losing any work.
  2. Step 2: Create a .gitignore file: If you haven’t already, create a .gitignore file in the root directory of your repository. This file will specify which files and directories should be ignored by Git.
  3. Step 3: Add the files to .gitignore: Open the .gitignore file and add the filenames or patterns of the files you want to untrack. For example, if you want to untrack a file named “example.txt,” add the following line to the .gitignore file:
example.txt

We can also use wildcards to match multiple files, for instance:

*.txt          # Ignore all files with a .txt extension
directory/     # Ignore the entire 'directory' and its contents

4.Step 4: Remove the files from Git cache: After adding the files to the .gitignore file, you need to remove them from the Git cache. You can do this using the git rm command with the --cached option. For example, to remove the “example.txt” file from the cache, run:

git rm --cached example.txt

If you want to remove multiple files, you can specify them in a single command:

git rm --cached file1.txt file2.txt directory/file3.txt

5.Step 5: Commit changes: After removing the files from the cache, commit the changes to the repository.

git commit -m "Untrack files specified in .gitignore"

That’s it! The files specified in the .gitignore file will no longer be tracked by Git, and they won’t appear in future commits. Be careful when using the git rm --cached command, as it will remove the files only from the index (staging area) and not from our working directory. In others words, the file should no longer be tracked by Git. However, it remains in our working directory. Note that this doesn’t make any changes to existing commits, it just prevents the file from being included in future commits.

We can now push the changes to our remote to see the changes effective there as well.

PS: Untracked files can make our git status output messy and confusing, and they can also waste disk space.

To remove untracked files in our working directories, we can use the git clean command. This command will delete any untracked files and directories in our working directory.

  • We can use the -n option to perform a dry run and see what files will be deleted without actually deleting them. 
  • We can use the -f option to force the deletion of the files.
  • We can use the -d option to also delete untracked directories.
  • We can use the -x option to also delete ignored files (files that match the patterns in your .gitignore file).

For example, to delete all untracked files and directories, including ignored ones, we can run:

git clean -d -x -f

By Shabazz

Software Engineer, MCSD, Web developer & Angular specialist

Leave a Reply

Your email address will not be published. Required fields are marked *