从git中删除未跟踪的更改

huangapple go评论72阅读模式
英文:

removing untracked change from git

问题

我会感激如果你能帮助我解决我在git中遇到的问题。我对使用git还很陌生。我需要移除git中的未跟踪更改。我已经尝试过"git checkout ."和"git restore .",但它们并不总是有效。是否有更可靠的方法?任何帮助都将不胜感激。

这是我需要从git中移除的未跟踪文件:

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        secret1.txt

 test-repo % git restore .
 test-repo % git status
HEAD detached from V2
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        secret1.txt

 test-repo % git checkout .
Updated 0 paths from the index
 test-repo % git status
HEAD detached from V2
Untracked files:
   (use "git add <file>..." to include in what will be committed)
        secret1.txt

nothing added to commit but untracked files present (use "git add" to track)

正如你所看到的,secret1.txt 仍然存在!

英文:

I would appreciate if you help me in this problem I have with git. I am new with working with that. I need to remove untracked change from git. I have already tried "git checkout ." and " git restore .", but those are not working always. Is there any more reliable way? Any help is appreciated.

This is the untracked file I need to be removed from git:

Untracked files:
  (use &quot;git add &lt;file&gt;...&quot; to include in what will be committed)
        secret1.txt

 test-repo % git restore .
 test-repo % git status
HEAD detached from V2
Untracked files:
  (use &quot;git add &lt;file&gt;...&quot; to include in what will be committed)
        secret1.txt

 test-repo % git checkout .
Updated 0 paths from the index
 test-repo % git status
HEAD detached from V2
Untracked files:
   (use &quot;git add &lt;file&gt;...&quot; to include in what will be committed)
        secret1.txt

nothing added to commit but untracked files present (use &quot;git add&quot; to track)

As you can see secret1.txt is still there!

答案1

得分: 2

Git命令:

  • git clean -f(移除所有未跟踪的文件)
  • git clean -f path/to/file(移除特定文件 path/to/file)
英文:

Git commands:

  • git clean -f (remove all untracked files)
  • git clean -f path/to/file (remove the specific file path/to/file)

答案2

得分: 0

"untracked changes" 是指 Git 完全不了解的文件。解决这个问题的最简单方式就是使用 rm secret1.txt 命令删除该文件。

英文:

"untracked changes" are by definition files which git knows nothing about. The easiest way to fix this is just to delete the file with rm secret1.txt.

huangapple
  • 本文由 发表于 2023年3月7日 14:50:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75658767.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定