简体   繁体   English

无法取消git提交的文件

[英]Unable to unstage git committed file

When we added a directory to git, we had some temporary files (ending in .~ ) that we don't want to track added already by mistake. 当我们向git添加一个目录时,我们有一些临时文件(以.~结尾),我们不想跟踪已经错误添加的文件。

We've since setup a .gitignore file with a *.~ rule. 我们已经设置了一个带有*.~规则的.gitignore文件。 This works for all new temporary files, but we're unable to remove the few that were already committed. 这适用于所有新的临时文件,但我们无法删除已经提交的少数文件。

When we run git rm --cached <file> it moves the file from modified: to a deleted: status. 当我们运行git rm --cached <file>它将文件从modified:移动到deleted: status。 And says 并说

# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       deleted:    FILE.~

But when we run git reset HEAD FILE.~ , it's still in the list of files under 但是当我们运行git reset HEAD FILE.~ ,它仍然在下面的文件列表中

Unstaged changes after reset:
M       pd/FILE.~

You're almost there: stop after step one and commit! 你几乎就在那里:在第一步之后停下来并提交!

The message given by git status is how to undo the change you've already made, if you made it in error. git status给出的消息是如何撤消已经进行的更改,如果您错误地进行了更改。 You've done the right thing in running git rm --cached <file> -- that command removes the file from your staging area, while leaving it present in your local copy. 你在运行git rm --cached <file>做了正确的事 - 该命令从暂存区域中删除文件,同时将其保留在本地副本中。 Git status tries to be helpful, by letting you know what you need to do if a file has an incorrect status. 如果文件的状态不正确,通过让您知道需要做什么,Git状态会有所帮助。 In this case, the file has the correct status so you can go ahead and commit. 在这种情况下,文件具有正确的状态,因此您可以继续并提交。

The problem is that stage is not what you think it is 问题是阶段不是你想象的那样 文件生命周期

Above image from the Git Book clearly shows the file statuses. Git Book上面的图像清楚地显示了文件状态。

The temporary files you already commited are not actually staged but they are tracked. 您已提交的临时文件实际上并未暂存,但会对其进行跟踪。 Git stops tracking files when commit a remove. Git在提交删除时停止跟踪文件。 git status says there is a change to be commited which is removing the file. git status表示要提交的更改正在删除文件。 git commmit -a -m "Removed temp files" should actually commit the remove and make the file untracked. git commmit -a -m "Removed temp files"应该实际提交删除并使文件未跟踪。

However, this won't remove the file from older commits for which I like to link people to Github Help: Remove Sensitive Data as it explains it very clearly. 但是,这不会从旧的提交中删除该文件,我希望将其链接到Github帮助:删除敏感数据 ,因为它非常清楚地解释了它。

git filter-branch --force --index-filter \\ 'git rm --cached --ignore-unmatch ' \\ --prune-empty --tag-name-filter cat -- --all git filter-branch --force --index-filter \\'git rm --cached --ignore-unmatch'\\ --proune-empty --tag-name-filter cat - --all

Above command will also purge the old files from history completely which you probably don't need but I included as extra information. 上面的命令也将完全从历史中清除旧文件,您可能不需要这些文件,但我将其作为额外信息包含在内。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM