DEV Community

AK DevCraft
AK DevCraft Subscriber

Posted on • Edited on

Untrack existing Git file

How would you stop tracking future changes in an existing Git repository file, but do not want to delete that file from the repository?
git rm --cached is the command that’ll do the trick, however, it needs to be done in following way.

Syntax

git rm --cached filename
Enter fullscreen mode Exit fullscreen mode

Steps with example

Update the .gitignore file to include the filename, stage and commit .gitignore file.

ak@--mac git-useful-commands % echo "db.config" >> .gitignore
ak@--mac git-useful-commands % git add .gitignore 
ak@--mac git-useful-commands % git commit -m "Update .gitignore"
Enter fullscreen mode Exit fullscreen mode

Now make some edits and stage the file that you want to remove from future tracking. Later, remove the file from staging and commit it

ak@--mac git-useful-commands % git add db.config 
ak@--mac git-useful-commands % git rm --cached db.config
ak@--mac git-useful-commands % git commit -m "untrack db config file"
Enter fullscreen mode Exit fullscreen mode

Now, the file will remain in the repository, but future changes won't be tracked.

Happy Coding!

Top comments (0)