简体   繁体   中英

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:

~/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. 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.

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. (ie just go git commit -m "SOME MESSAGE" )

git commit -a says to automatically stage files that have been modified and deleted. When you are trying to remove a file from git that may still exist locally this will be problematic.

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.

Another option provided by git help rm is as follows:

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 diff --name-only --diff-filter=D -z | xargs -0 git rm --cached 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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