Git "forget" already tracked files

Scenario: A project is initialized, add a .gitignore file with content .idea. But unfortunately the folder idea is already in the git repo, generate the .gitignore file later will not remove the folder from the repo.

Solutions:

1.

Make sure the working space is clean

$ git rm -r --cached .idea/
rm '.idea/.gitignore'
rm '.idea/UpdateTvtsPkg.iml'
rm '.idea/dictionaries'
rm '.idea/inspectionProfiles/Project_Default.xml'
rm '.idea/inspectionProfiles/profiles_settings.xml'
rm '.idea/misc.xml'
rm '.idea/modules.xml'
rm '.idea/vcs.xml'
git add -u
git commit -m "Update git ignored files/folders"
git push

2.

Make sure the working space is clean

# Remove all tracked files, including wanted and unwanted
git rm -r --cached .
# Add all the files/folders expect those in .gitignore
git add .
git commit -m "Update git ignored files/folders"
git push

.gitignore will prevent untracked files from being added (without an add -f) to the set of files tracked by Git. However, Git will continue to track any files that are already being tracked.

WARNING: While this will not remove the physical file from your local machine, it will remove the files from other developers' machines on their next git pull.
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容