Remove commited file from git repo keeping local copy

James Bhatta
Dec 21, 2020
  • Create a .gitignore if you have not one already
  • Edit .gitignore and add the path/to/file
  • Execute git -rm --cached path/to/file . All the removed files will be listed. The --cached file specifies that you want to remove it from repository but keep the local copy. Also you can use -r flag to remove files recursively.c
  • Run git-status to verify the changes.
  • Push the code to repository using git push.

Note: while you ignored those files from repository, they can still be viewed from history. Thus if you want them to be excluded from history as well, make sure you remove them from Git History.

--

--

James Bhatta

Manoj Bhatta. PHP developer often working in Laravel. Loved PHP just because I cant wait until compiler processes my code.