【问题标题】:Unable to discard all local files in git无法丢弃git中的所有本地文件
【发布时间】:2014-05-28 15:48:03
【问题描述】:

我的工作目录中有 2 个文件。我想完全丢弃这些文件,以便可以切换分支。

网上有很多有用的指南,我已经尝试了所有可以找到的答案来删除这些文件,但它们仍然存在。

user@machine:~/Code/foo$ 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:   src/test/foo.c
    modified:   src/test/bar.c

我根据其他 stackoverflow 问题尝试过的命令。

 git reset 

 git reset --hard HEAD^

 git stash save --keep-index
 git stash drop

 git checkout HEAD -- $(git ls-files -m)

 git clean -f
 git clean -dfx

 git checkout -- .

 git checkout -- *

 git checkout 123456

 git reset --hard 123456

 git reset HEAD --hard

令人惊讶的是,在每个命令之后,我发现文件仍然存在!

user@machine:~/Code/foo$ 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:   src/test/foo.c
    modified:   src/test/bar.c

由于只有 2 个文件,我知道我可以 一个一个地重新检查它们。但是,如果再次发生这种情况并且我有 100 个文件,我想知道如何将所有文件一次扫除。

 git checkout src/test/foo.c
 git checkout src/test/bar.c

更新

显然,甚至 git checkout 都不起作用

git checkout src/test/foo.c
echo $?
0
git checkout src/test/bar.c
echo $?
0

user@machine:~/Code/foo$ 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:   src/test/foo.c
    modified:   src/test/bar.c

这些文件不会消失。

更新2

我也试过这些命令,但没有运气。

git reflog
git checkout HEAD@{17}

git init
git reset --hard HEAD^

更新3

存储没有任何作用。

git checkout master
git stash
git stash pop
git stash drop

删除文件也不起作用。

user@machine:~/Code/foo$ git rm --cached $(git ls-files -m)
rm 'src/test/foo.c'
rm 'src/test/bar.c'

user@machine:~/Code/foo$ git status
On branch master
Your branch is behind 'origin/master' by 162 commits, and can be fast-forwarded.
(use "git pull" to update your local branch)

Changes to be committed:
(use "git reset HEAD <file>..." to unstage)

    deleted:   src/test/foo.c
    deleted:   src/test/bar.c

【问题讨论】:

标签: git


【解决方案1】:

这听起来很像行尾问题,您已经告诉 git 在签入时进行自动转换,而这些转换的结果与提交状态不匹配。检查.git/info/attributes.gitattributestext 属性:

text

This attribute enables and controls end-of-line normalization.When a text file is normalized, its line endings are converted to LF in the repository.

如果有人签入了包含 CRLF 的文件,我知道 text=auto 是一个好方法,呃,找到它们。

【讨论】:

  • 很高兴知道,我的 info 目录中似乎没有属性文件。
【解决方案2】:

对于所有未暂存的文件,请使用(注意末尾的句点)。这对我有用(从 gi​​t v 1.8.4.5 开始)

git checkout -- .

或者你可以把所有东西都藏起来;

git stash

这样你仍然可以回到原来的状态,或者你甚至可以将这些更改应用到另一个分支:

git stash pop (this command applies the latest stash)

或者如果您不再需要更改:

git stash drop

【讨论】:

  • 我已经尝试了所有 4 个建议,但文件仍然存在
  • 好像有什么奇怪的事情发生了……你在哪个分支?执行“git branch -a”...也许你处于“分离头”状态?
  • Chris Maes awswer 是最好的选择,把它们藏起来可以解决你的问题。
  • 我在 master 分支上,@KSNidhin 我试过 git stash 没有运气
  • 如果你执行 git diff,你的输出是什么?
【解决方案3】:

想通了。

在 git diff 中,它显示文件名是小写的foo.bar,但是当我实际检查文件时,它是Foo.bar

我使用的是不区分大小写的文件系统的 mac。

我应该可以通过手动移动文件来修复它

Case sensitivity in Git

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-06-01
    • 2011-07-03
    • 1970-01-01
    • 2019-01-14
    • 2011-04-22
    • 2022-11-17
    • 2010-12-07
    • 1970-01-01
    相关资源
    最近更新 更多