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:
Step 2: Create New File
Then, create/make a new file in the current repository:
Step 3: Check Git Status
Next, view the current repository’s 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:
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:
Alternatively, you can utilize the “–skip-worktree” option along with the “git update-index” command to perform the same operation:
Step 6: Make Changes in File
Next, make changes in the desired file by updating its content:
Step 7: Ensure Changes
Lastly, verify whether Git has ignored the changes in the selected file or not by checking the repository’s 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.