Git push 不会忽略 .gitignore 中的文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20966751/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 09:32:51  来源:igfitidea点击:

Git push doesn't ignore files in .gitignore

gitgithub

提问by Haiyang

I committed some files that I shouldn't commit. Then I added these files in .gitignore and I did this:

我提交了一些我不应该提交的文件。然后我在 .gitignore 中添加了这些文件,我这样做了:

git rm -r --cached somefile

and I committed again.

我又犯了。

git statusshows:

git status显示:

# On branch experiment
nothing to commit, working directory clean

# On branch experiment
nothing to commit, working directory clean

I do git ls-filesand these files are not listed.

我这样做了git ls-files,但没有列出这些文件。

But when I do git push origin somebranch, these files are still being pushed (they're too large to push to github so it fails).

但是当我这样做时git push origin somebranch,这些文件仍在被推送(它们太大而无法推送到 github 所以它失败了)。

why is this? what should I do?

为什么是这样?我该怎么办?

回答by AD7six

The files are still in your git history. From what's in the question the git history is for example:

这些文件仍在您的 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

And pushing fails because of pushing the contents of commit bbbbbbb, it doesn't matter that the file is not in your currently checked out version it's in the project history.

由于推送 commit 的内容,推送失败bbbbbbb,文件不在您当前检出的版本中并不重要,它在项目历史记录中

The above can also be confirmed by checking for the history of a specific file/path:

以上也可以通过检查特定文件/路径的历史记录来确认:

$ git log --all --format=%H -- big.file
aaaaaaa Update .gitignore and remove big files
bbbbbbb accidentally add big files to repo

If big.fileis large enough to prevent pushing to github - you need that list of commits to be empty

如果big.file足够大以防止推送到 github - 您需要该提交列表为空

Obliterating a file from history

从历史记录中删除文件

There are a number of ways to prevent attempting to push big.fileto the remote, but based on the info in the question the simplest of which is:

有多种方法可以防止尝试推big.file送到遥控器,但根据问题中的信息,最简单的是:

$ git reset --hard ccccccc

This will remove commits aaaaaaaand bbbbbbbthus there will be no attempt to push big.fileto the remote.

这将删除提交aaaaaaabbbbbbb因此不会尝试推big.file送到远程。

The purpose of .gitignore

.gitignore 的目的

.gitignorefiles affect addingfiles to the repository, they do not affect files already in the index. This is the reason that adding files to .gitignorehas had no effect on 'the problem'.

.gitignorefiles 影响存储库添加文件,它们不影响 index 中已有的文件。这就是添加文件.gitignore对“问题”没有影响的原因。

回答by atupal

If you have commited the file . You can cancel the lastest commit bu using:

如果您已提交文件 . 您可以使用以下方法取消最新的提交 bu:

git reset --soft HEAD^

And then:

进而:

git reset filename

And then add the file to .gitignore:

然后将文件添加到 .gitignore:

or:

或者:

git update-index --assume-unchanged  filename

回答by matrik

The solutions here will work but you will lose other changes in the same commit. If you don't want to lose the other changes in the same commit, and you didn't push the commit yet, you have to change HEAD back to a previous commit, add the changed file and amend:

此处的解决方案将起作用,但您将丢失同一提交中的其他更改。如果您不想丢失同一提交中的其他更改,并且您还没有推送提交,则必须将 HEAD 更改回之前的提交,添加更改的文件并修改:

First find which commit you would like to return to:

首先找到您要返回的提交:

$ git log --oneline

Then, it's needed to checkout another branch because you cannot change the HEAD in the active branch:

然后,需要检出另一个分支,因为您无法更改活动分支中的 HEAD:

$ git checkout master

Then, change the head of the branch to an older commit:

然后,将分支的头部更改为较旧的提交:

$ git branch -f <branch_name> <commit_id>

Then, add the changed file to current commit:

然后,将更改的文件添加到当前提交:

$ git add <filename>

Then, amend the change into current commit:

然后,将更改修改为当前提交:

$ git commit --amend --no-edit

That multi-step solution worked out for me to not lose changes and add new changes to a previous commit.

这个多步骤的解决方案让我不会丢失更改并将新更改添加到以前的提交中。