如何在Git中不添加未跟踪的文件?

4
我正在摆弄代码,现在不想提交这些文件。如何将其从要提交的文件列表中删除?
git status 
On branch sign-up
Untracked files:
  (use "git add <file>..." to include in what will be committed)

    HEAD
    description
    hooks/
    info/
    sample_app/

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

1
Git 只提交“暂存区”中的文件,这些文件是您自己添加的文件。而“未跟踪的文件”是您已更改但未添加到暂存区的文件。它们不会被提交,但由于它们已更改,Git 将继续提醒您(除非您在 .gitignore 中明确忽略它们)。 - Steve D
1
这些文件与Git存储库的.git/目录中发现的文件非常相似,这似乎有点可疑。您是否在.git目录内执行了git init命令? - Schwern
2
@SteveD,除了它们并没有被“更改”之外,其他都是正确的。Git不跟踪这些文件,因此无法知道它们是否已更改。它们只是Git未跟踪的文件和目录。 - Schwern
@schwern:我的错误,之前想的是已经提交的文件。 - Steve D
5个回答

3

现在,这些文件因为是未跟踪的,所以不会被提交,Git会忽略它们。

在你的情况下,git status只是告诉你,如果你想的话,可以跟踪这些文件(并将它们添加到下一次提交中)。


2
如果您希望Git不再跟踪这些文件,请将它们添加到.gitignore中,Git将忽略这些文件和/或目录。在此链接中了解有关在Git中忽略文件的更多信息。
但是,如果您将来要提交它们,则可以选择不将它们添加到.gitignore中。Git将继续引发此警告,直到您选择跟踪或忽略它们。

1
如果您不希望这些文件被意外添加,第一个选择是将它们添加到您的.gitignore文件中Pro Git中的详细信息)。您的.gitignore文件可能很简单:
HEAD
description
hooks/
info/
sample_app/

请注意,如果您不需要这些文件,请将.gitignore文件添加到您的代码库中。
你提到的文件是“未跟踪”的。正如你的git状态命令输出所说,这些文件没有被添加到提交中。(“nothing added to commit but untracked files present (use "git add" to track)”)
Git使用一个“跟踪-暂存-提交”的循环来跟踪你的文件(Pro Git中的细节)。只有当文件被“暂存”时,它们才会被提交到Git仓库。
作为我的个人实践,我通常从GitHub提供的.gitignore文件开始,然后在gitignore文件的顶部添加一些特定于repo的目录和文件,并加上注释,以便清楚地了解仓库中被忽略的内容。
# Repo specific settings
wiki/
test/

# Object files
*.o
*.ko

# Libraries
*.lib
*.a
*.la
*.lo

请记住,如果您想将新文件添加到.gitconfig文件中,即使它被标记为被忽略,您也需要使用git命令git add -f <文件名> - Casey Robinson
1
没错。我还想指出,你可能会发现一些文件已经被 Git 跟踪了,然后将它们添加到 .gitignore 中并不能将其从跟踪中移除。使用 git rm [--cached] <文件名> 将文件从 Git 的跟踪中移除,然后 .gitignore 就可以正常工作,防止这些文件再次被跟踪。 - Mo Frank Hu

0

你目前正在处理新文件,所以它们处于未跟踪的文件状态。

  • 这意味着Git实际上看到了你的新文件,但在你执行git add之前不会对其进行处理。

  • 当你执行git add后,你的文件会进入未修改的文件状态。之所以是未修改,是因为这是一个没有之前版本的新文件。基本上,Git以前从未跟踪过任何更改。

这是一个很好的链接,可以让你熟悉Git的文件状态生命周期。Git文件状态生命周期


-1
如果您希望Git不再跟踪这些文件,请将它们添加到.gitignore中,例如,一个Android Studio项目的示例.gitignore文件可以如下所示:
# built application files
*.apk
*.ap_

# files for the dex VM
*.dex

# Java class files
*.class

# generated files
bin/
gen/

# Local configuration file (sdk path, etc)
local.properties


#Eclipse
*.pydevproject
.project
.metadata
bin/**
tmp/**
tmp/**/*
*.tmp
*.bak
*.swp
*~.nib
local.properties
.classpath
.settings/
.loadpath
YourProjetcName/.gradle/
YourProjetcName/app/build/
*/YourProjetcName/.gradle/
*/YourProjetcName/app/build/

# External tool builders
.externalToolBuilders/

# Locally stored "Eclipse launch configurations"
*.launch

# CDT-specific
.cproject

# PDT-specific
.buildpath

# Proguard folder generated by Eclipse
proguard/

# Intellij project files
*.iml
*.ipr
*.iws
.idea/
/build
build/
*/build/
*/*/build/
*/*/*/build/
*.bin
*.lock
YourProjetcName/app/build/
.gradle
/local.properties
/.idea/workspace.xml
/.idea/libraries
.DS_Store
.gradle/
app/build/
*app/build/

# Local configuration file (sdk path, etc)
local.properties
/YourProjetcName/build/intermediates/lint-cache/api-versions-6-23.1.bin
appcompat_v7_23_1_1.xml
projectFilesBackup
build.gradle
YourProjetcName.iml
YourProjetcName.iml
gradlew
gradlew.bat
local.properties
settings.gradle
.gradle
.idea
android
build
gradle

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接