Delete Files From The History In Git

Author Avatar
Young Hug Nov 11, 2020

We need to delete files from the history of git when we commit some files which shouldn’t be committed.

Steps

There are two steps to delete files from git.

Delete Files

There are two options for us to delete files.

  • Delete a single file
1
git filter-branch --index-filter 'git rm --cached --ignore-unmatch Path/YourFileName'
  • Delete a folder
1
git filter-branch --index-filter 'git rm --cached -r --ignore-unmatch ThePathOfTheFolders'

Push change to remote repo

The command to push change to remote repo is simple. The command is git push -f.

Clear Local Git

1
2
3
4
5
6
7
8
9
10
11
12
13
14
rm -rf .git/refs/original/

#Expire reference logs
git reflog expire --expire=now --all

#验证数据库中对象的连通性和有效性
#打印出存在但不能从任何参考节点到达的对象。
git fsck --full --unreachable

#打包之后,如果新创建的包使某些现有包冗余,请移除冗余包
git repack -A -d

#清理不必要的文件并优化本地存储库
git gc --aggressive --prune=now

This blog is under a CC BY-NC-SA 3.0 Unported License
Link to this article: https://younggod.netlify.app/2020/11/11/Practice/deletFileFromGit/