Assume that, you have committed and pushed a file thelinuxfaq.txt or any directory node_modules to the Git repository and if you want to ignore that file or not required to keep on the remote repository,
To ignore a file that has already been committed to a Git repository, you will need to delete the file from your repository and then add a rule to your .gitignore file to ignore the file in the future. Here are the steps to do this:
Remove the file from your repository by running the following command:
git rm --cached < file path >
Add the file to your .gitignore file so it will be ignored in the future.
git add < file path >
Commit your changes to the repository:
git commit -m "Remove file from repository"
Note: Add "-r" after "rm" if you want do it for the directory
Add the file / path into .gitignore file and do this.
git add .gitignore
git commit -m "Adding <file> to git ignore"
git commit -m "Adding <file> to git ignore"
Push your changes to the remote repository:
git push
or
git push origin <branh name>
Below is full steps to remove a directory or file path "nodejs_app/node_modules" from git repo, assume that I've used the project/git repo root path.
git rm --cached -r nodejs_app/node_modules
git rm --cached -r thelinuxfaq.txt
git status
git commit -m "remove the node_module and a thelinuxfaq.txt"
echo "/apk/android_app/node_modules" >> .gitignore
git status
git add .gitignore
git commit -m "Adding node modules and thelinuxfaq.txt to git ignore"
git push
Keep in mind that this will remove the file from the repository for all users. If you want to keep the file locally but ignore it for everyone else,
you can simply add the file to your .gitignore file and leave it in the repository.
Comments (0)