把某些目录或文件加入忽略规则发现并未生效,原因是.gitignore
只能忽略那些原来没有被追踪的文件,如果某
些文件已经被纳入了版本管理中,则修改.gitignore
是无效的。那么解决方法就是先把本地缓存删除(改变成未
被追踪状态),然后再提交:
1
2
3
| git rm -r --cached .
git add .
git commit -m 'update .gitignore'
|
使用场景:一不小心把 node_modules 目录加入git仓库了,导致仓库很大
核弹级操作,注意备份
1
| git filter-branch -f --prune-empty --index-filter "git rm --cached --ignore-unmatch -fr node_modules" -- --all
|
1
2
3
4
| git verify-pack -v .git/objects/pack/pack-b39c5c51f10088875c96f1073840442c0d08f368.idx | sort -k 3 -n | tail -3
9ab9f94ac7d6fea2bdf444e88104bd72670a70b5 blob 46247335 8791786 13950107
b3c7fb8da2fdf8a1f0e906656271db029baf44ce blob 56742730 54432739 101022428
ca099a126474f353ed6a9a36edd34af3bca7d5f1 blob 61498722 8419597 28982757
|
1
| git rev-list --objects --all | grep 9ab9f94ac7d6fea2bdf444e88104bd72670a70b5
|
将该文件从历史记录的所有 tree 中移除
1
2
3
4
5
| git filter-branch --index-filter 'git rm -rf --cached --ignore-unmatch app/build'
#git filter-branch -f --prune-empty --index-filter "git rm -rf --cached --ignore-unmatch app/build" -- --all
#git filter-branch -f --prune-empty --index-filter 'git rm -rf --cached --ignore-unmatch app/build' --tag-name-filter cat -- --all
|
1
2
3
4
5
| rm -rf .git/refs/original/
git reflog expire --expire=now --all
git fsck --full --unreachable
git repack -A -d
git gc --aggressive --prune=now
|
1
| git push origin --all --force
|
在很多情况,我们由于疏忽会将一些敏感信息误传到Github上面去。 尽管我们可以使用git rm
将包含敏感信息文件删除掉,然后重新提交上传,文件就不会在 GitHub 中的文件列表显示。 但是这并不能完全将敏感信息文件从 GitHub 中完全删除,commit history
仍然会有敏感信息的文件的残留,我们仍然可以从 GitHub 中的commit history
中访问到文件。
如果想要将敏感信息文件完全删除。不仅需要将文件从 GitHub 中的文件列表中删除,同时还需要将文件从 GitHub 的commit history
中的 文件相关信息删除。
删除commit history
文件相关信息,主要有两种方法:
本文只探讨filter-branch的使用
使用 git 自带的git filter-branch
命令来实现。
此命令会将当前存在的文件也删除,注意备份。
1
| git filter-branch --force --index-filter 'git rm --cached --ignore-unmatch PATH-TO-YOUR-FILE-WITH-SENSITIVE-DATA' --prune-empty --tag-name-filter cat -- --all
|
1
| git filter-repo --invert-paths --path PATH-TO-YOUR-FILE-WITH-SENSITIVE-DATA
|
上传覆盖 GitHub 的commit history
1
| git push origin --force --all # (--all 作用于所有分支)
|
PATH-TO-YOUR-FILE-WITH-SENSITIVE-DATA 是你需要删除的敏感信息文件名
例如:
1
2
3
| git filter-branch --force --index-filter 'git rm --cached --ignore-unmatch test/zhangsan.txt' --prune-empty --tag-name-filter cat -- --all
--
git filter-repo --invert-paths --path test/zhangsan.txt
|
以上命令就会删除test/zhangsan.txt
先cd到根目录,执行git config --global credential.helper store
命令
1
| # git config --global credential.helper store
|
执行之后会在.gitconfig文件中多加红色字体项
1
2
3
4
5
| [user]
email = 1624717079@qq.com
name = bwcx
[credential]
helper = store
|
之后cd到项目目录,执行git pull
命令,会提示输入账号密码。输完这一次以后就不再需要,并且会在根目录生成一个.git-credentials
文件
1
2
| # git pull
# cat .git-credentials
|
之后pull/push
代码都不再需要输入账号密码了~
如果这个时候,还是提示需要输入账号,那就要进项目的根目录,.git 目录的 config里 找到
1
2
| [remote "origin"]
url = http://192.168.1.10/test/test.git
|
修改这里的url