To remove a file from git and add it as git ignored file we are going to do following steps:
  1. Remove the file from remote repository
  2. Add the changes and commit
  3. Add the same file in git ignore and commit the .gitignore file

Removes the file from the repository without physically deleting the file (that’s what the --cached does) in your local copy

git rm --cached < file path >

Add the changes to repo and push the changes

git add < file path >
git commit -m "<message>"
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"

Below is full example to remove a directory path "apk/android_app/node_modules" from git repo ( Assuming you are in project root path )

git rm --cached -r  apk/android_app/node_modules
git status
git commit -m "Removing node modules"
echo "/apk/android_app/node_modules" >> .gitignore
git status
git add .gitignore
git commit -m "Adding node modules to git ignore"
git push

 



Comments (0)
Leave a Comment

loader Posting your comment...