我提交了一些我不应该提交的文件。 然后我在 .gitignore 中添加了这些文件,然后我这样做了:
git rm -r --cached somefile
我再次 promise 。
git status
显示:
# On branch experiment<br/>
nothing to commit, working directory clean
我愿意git ls-files
并且这些文件未列出。
但是当我做 git push origin somebranch
,这些文件仍在推送中(它们太大而无法推送到 github,因此失败)。
这是为什么呢?我该怎么办?
最佳答案
这些文件仍在您的 git 历史记录中。例如,根据问题中的内容,git 历史是:
$ git log --oneline
aaaaaaa Update .gitignore and remove big files
bbbbbbb accidentally add big files to repo
ccccccc update foo
ddddddd update bar
推送失败是因为推送了提交 bbbbbb
的内容,文件不在您当前 checkout 的版本中并不重要它在项目历史中 .
上述内容也可以通过检查特定文件/路径的历史记录来确认:
$ git log --all --format=%H -- big.file
aaaaaaa Update .gitignore and remove big files
bbbbbbb accidentally add big files to repo
如果 big.file
足够大以防止推送到 github - 你需要提交列表为空
从历史中删除一个文件
有很多方法可以防止尝试将 big.file
推送到远程,但根据问题中的信息,最简单的方法是:
$ git reset --hard ccccccc
这将删除提交 aaaaaaa
和 bbbbbbb
因此不会尝试将 big.file
推送到远程。
.gitignore 的目的
.gitignore
文件影响添加 文件到存储库,它们 do not affect files already in the index .这就是将文件添加到 .gitignore
对“问题”没有影响的原因。
关于Git push 不会忽略 .gitignore 中的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20966751/