Git

How to Stop Tracking and Ignore Changes to a File in Git

In Git, a single repository contains several files. Users make changes in files over time and add those changes to the Git index to track those modifications. However, some files contain confidential information that developers do not want to commit to the public repository or share with other team members. In this situation, Git permits you to stop tracking and ignore modifications to a file in Git.

This article will illustrate the process to stop tracking and ignore changes to a particular file in Git.

How to Stop Tracking and Ignore Modifications to a Specific File in Git?

To stop tracking and ignore modifications to a particular file in Git, check out the following steps:

  • Redirect to the local repository.
  • Create a new file.
  • Stage and commit a new file.
  • Ignore file changes using the “git update-index –assume-unchanged <file-name>” command.
  • Ensure changes.

Step 1: Switch to Local Repository

First, redirect to the desired local repository:

cd "C:\Git\demo_Repo"

Step 2: Create New File

Then, create/make a new file in the current repository:

touch demo.txt

Step 3: Check Git Status

Next, view the current repository’s status:

git status

In the below output, the Git status indicates that the newly created file is untracked and uncommitted:

Step 4: Track and Commit File

Now, stage and commit the newly created file using the below-provided command:

git add demo.txt && git commit -m "demo.txt file added"

Step 5: Stop Tracking and Prevent Changes to File

Execute the following command along with the desired file name to prevent Git from detecting modifications in it:

git update-index --assume-unchanged demo.txt

Alternatively, you can utilize the “–skip-worktree” option along with the “git update-index” command to perform the same operation:

git update-index --skip-worktree demo.txt

Step 6: Make Changes in File

Next, make changes in the desired file by updating its content:

echo "This is demo file" >> demo.txt

Step 7: Ensure Changes

Lastly, verify whether Git has ignored the changes in the selected file or not by checking the repository’s status:

git status

As you can see, the repository’s status did not show the modified changes of the file which mean Git has ignored the file’s changes:

We have explained the easiest method to stop tracking and ignore modifications to a specific file in Git.

Conclusion

To stop tracking and ignore modifications/changes in Git, utilize the “git update-index –assume-unchanged <file-name>” command. Moreover, the “–skip-worktree” option can also be used with the “git update-index” command with the desired file name to ignore its changes. This article illustrated the method to stop tracking and ignore changes to a particular file in Git.

About the author

Laiba Younas

I have done bachelors in Computer Science. Being passionate about learning new technologies, I am interested in exploring different programming languages and sharing my experience with the world.