简体   繁体   English

git不会提交,因为删除的文件不存在

[英]git won't commit because a deleted file doesn't exist

It appears that I've somehow gotten git into a state in which it won't commit because a file that's been deleted doesn't exist: 似乎我已经以某种方式将git置于一个不会提交的状态,因为已删除的文件不存在:

~/src$ git status -u
# On branch master
# Your branch is ahead of 'origin/master' by 5 commits.
#   (use "git push" to publish your local commits)
#
# Changes to be committed:
#   (use "git reset HEAD ..." to unstage)
#
#   deleted:    release-vars.sh
#
~/src$ git commit -a
fatal: pathspec 'release-vars.sh' did not match any files
~/src$ ls release-vars.sh
ls: cannot access release-vars.sh: No such file or directory

Any ideas on how to resolve this situation? 关于如何解决这种情况的任何想法?

-a is explicitly telling it to commit the current version of every currently-tracked file from the content in your worktree. -a明确告诉它从工作树中的内容提交每个当前跟踪文件的当前版本。 It's not quite the same operation as 它的操作并不完全相同

git add --all

which might be what you're after here. 这可能就是你在这里所追求的。 Then do an ordinary commit. 然后做一个普通的提交。

If your .gitignore specs leave unignored detritus you don't want to track you could instead git rm --cached the deleted file explicitly so your subsequent git commit -a doesn't trip over the unexpectedly missing file. 如果您的.gitignore规范留下未经标记的碎屑,您不想跟踪您可以改为git rm --cached显式删除已删除的文件,以便后续的git commit -a不会跳过意外丢失的文件。

I would suggest starting by staging your changes manually using git add FILENAME and git rm FILENAME before doing a commit without the -a option. 我建议首先使用git add FILENAMEgit rm FILENAME手动暂存您的更改,然后再执行提交而不使用-a选项。 (ie just go git commit -m "SOME MESSAGE" ) (即只是去git commit -m "SOME MESSAGE"

git commit -a says to automatically stage files that have been modified and deleted. git commit -a表示自动git commit -a已修改和删除的文件。 When you are trying to remove a file from git that may still exist locally this will be problematic. 当您尝试从本地可能仍然存在的git中删除文件时,这将是有问题的。

Being explicit about what you are doing is rarely a bad thing and may easily resolve the problem. 明确你正在做的事情很少是坏事,可以很容易地解决问题。

If that doesn't work; 如果这不起作用; try doing git reset and then try this fresh. 尝试做git reset然后尝试新鲜。

Another option provided by git help rm is as follows: git help rm提供的另一个选项如下:

If all you really want to do is to remove from the index the files that are no longer present in the working tree (perhaps because your working tree is dirty so that you cannot use git commit -a), use the following command: 如果您真正想要做的就是从索引中删除工作树中不再存在的文件(可能是因为您的工作树是脏的,因此您不能使用git commit -a),请使用以下命令:

  git diff --name-only --diff-filter=D -z | xargs -0 git rm --cached 

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

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