Ignoring Files after Initial Commit
Swipe to show menu
If you've already committed files that you now want to ignore, follow these steps:
- Add the files to your
.gitignore; - Remove the files from the repository (without deleting them locally) using
git rm --cached; - Commit the changes.
Workflow Example
Configuration files often contain private information, so they should not be tracked.
To prevent accidentally adding such files to Git, it’s best to ignore them.
Apply this to the config.txt file, which has already been committed.
Refer to the following illustration of the workflow:
Add config.txt to the list of ignored files in the .gitignore file:
echo config.txt >> .gitignore
Then run the git rm command with the --cached flag to remove the file from the repository without deleting it locally:
git rm --cached config.txt
Now check the status of the working tree and staging area:
git status
As you can see, the deletion of the config file is already staged, but the .gitignore file still needs to be added.
After that, commit both changes:
git add .gitignore
git commit -m "Add the config.txt file to gitignore"
The commit is successful.
Now verify that the config.txt file is ignored by appending a new line with an example password to it using the echo command:
echo "PASSWORD=qwerty" >> config.txt
Finally, check the status of the working tree:
git status
The file was modified, but it is not tracked by Git, so the working tree remains clean.
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat