git 无法摆脱“未为提交而暂存的更改”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13630761/
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 07:49:49  来源:igfitidea点击:

Can't get rid of "Changes not staged for commit'

git

提问by FractalSpace

I am unable to get rid of this state in which my repo is seem to be locked in. After a doing a reset to HEAD~1, I keep getting this notification about this single file being modified. 'add' and 'checkout' have not affect. I have core.autocrlf and core.safecrlf unset (empty).

我无法摆脱我的 repo 似乎被锁定的这种状态。在重置为 HEAD~1 后,我不断收到有关此单个文件被修改的通知。'add' 和 'checkout' 没有影响。我有 core.autocrlf 和 core.safecrlf 未设置(空)。

Please see below:

请参阅以下内容:

$ git --version
git version 1.7.9.6 (Apple Git-31.1)

$ git status

# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       modified:   a_file_name.cpp

The followings commands (ran individually) have no affect:

以下命令(单独运行)没有影响:

$ git checkout -- a_file_name.cpp
$ git reset a_file_name.cpp
$ git add a_file_name.cpp
$ git reset --hard
$ git clean -n
<nothing>
$ git clean -f
<nothing>

$ git status

# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       modified:   a_file_name.cpp

and it goes on ...

它继续......

What did I do wrong?

我做错了什么?

Response to @Don's suggestion below (git rm), no change, but here is how it goes:

对下面@Don 的建议 (git rm) 的回应,没有变化,但它是如何进行的:

$ git rm 
error: 'a_file_name.cpp' has local modifications
(use --cached to keep the file, or -f to force removal)
$ git rm -f a_file_name.cpp
rm 'a_file_name.cpp'
$ git status

# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       deleted:    a_file_name.cpp
#
# Changes not staged for commit:
#   (use "git add/rm <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       deleted:    a_file_name.cpp
#

$ git commit -m"tmp"
[master 2a9e054] tmp
1 file changed, 174 deletions(-)
delete mode 100644 a_file_name.cpp

$ git status
# Your branch is ahead of 'origin/master' by 1 commit.
#
# Changes not staged for commit:
#   (use "git add/rm <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       deleted:    a_file_name.cpp
#

Pretty much back to sq.1

几乎回到了 sq.1

采纳答案by Code-Apprentice

Be sure that you are in the same directory as the file when you run any gitcommands. Alternatively, you can use a relative or absolute path for files used with gitcommands. The output from git statusshouldindicate the subdirectory where the file is located. I find it strange that the output you posted here doesn't show that.

运行任何git命令时,请确保您与文件位于同一目录中。或者,您可以对与git命令一起使用的文件使用相对或绝对路径。的输出git status指示文件所在的子目录。我觉得奇怪的是,您在此处发布的输出并未显示这一点。

回答by Goulart

git commit -a -m "message"

The -a option will add all tracked files with modifications

-a 选项将添加所有经过修改的跟踪文件

回答by farmi

A round about way that worked for me, is to:

一种对我有用的方法是:

  1. recreate the file that I deleted.

  2. git add path/filename

  3. git rm --cached path/filename

  4. delete the file

  5. git add .

  6. git commit --amend # if not on an already pushed branch.

  1. 重新创建我删除的文件。

  2. git add path/filename

  3. git rm --cached path/filename

  4. 删除文件

  5. git add .

  6. git commit --amend # if not on an already pushed branch.

回答by jeremy

Do you need any of the changes in the modified file? git-reset by default leaves the files in the directory tree.

您需要对修改后的文件进行任何更改吗?git-reset 默认将文件留在目录树中。

git reset --hard

will reset and write over the files in your working tree with the files in the commit.

将使用提交中的文件重置并覆盖工作树中的文件。

Have you done a git diff after each of the steps to see if any changes actually exist?

您是否在每个步骤之后都执行了 git diff 以查看是否确实存在任何更改?