让 git 不显示未跟踪的文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2774416/
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
Get git not to show untracked files
提问by chiggsy
When doing git commit
, is there a way to not display the untracked files in my editor (defined in $EDITOR
)? I know how to do so in the shell (git status -uno
), but I'd like do it in editor as well.
这样做时git commit
,有没有办法不在我的编辑器中显示未跟踪的文件(在 中定义$EDITOR
)?我知道如何在 shell ( git status -uno
) 中执行此操作,但我也想在编辑器中执行此操作。
Note that I do not want to ignore these files forever; I just don't want to see them on certain occasions.
请注意,我不想永远忽略这些文件;我只是不想在某些场合看到他们。
采纳答案by J. Cordasco
From the git-commitman page:
从git-commit手册页:
-u[], --untracked-files[=] Show untracked files (Default: all). The mode parameter is optional, and is used to specify the handling of untracked files. The possible options are: · no - Show no untracked files · normal - Shows untracked files and directories · all - Also shows individual files in untracked directories. See git-config(1) for configuration variable used to change the default for when the option is not specified.
回答by mipadi
If you don't everwant to commit them to your repo, use a .gitignore
file to ignore them. More details can be found on the gitignore
man page. They won't show up as untracked files when entering your commit message in your $EDITOR
.
如果你不以往任何时候都希望将其提交到您的回购,使用.gitignore
文件忽略它们。可以在gitignore
手册页上找到更多详细信息。在$EDITOR
.
If you simply don't want to see them when committing, set the Git config variable status.showUntrackedFiles
to no
, as noted here:
如果你根本不希望看到他们的时候犯,设置的Git配置变量status.showUntrackedFiles
,以no
作为注意这里:
$ git config --global status.showUntrackedFiles no
回答by Lohrun
You can temporary use the git commit option -uno
to mask untracked files (git help commit
).
您可以临时使用 git commit 选项-uno
来屏蔽未跟踪的文件 ( git help commit
)。
If you want a permanent solution use the .gitignore
file.
如果您想要永久解决方案,请使用该.gitignore
文件。
For instance, if you want to ignore the file bar.foo
and any file with the .bak
extension, you juste have to create a .gitignore
file in the root directory of your project containing :
例如,如果您想忽略该文件bar.foo
和任何带有.bak
扩展名的文件,您必须.gitignore
在项目的根目录中创建一个包含以下内容的文件:
bar.foo
*.bak
Some file are ignored by a global gitignore
file (for instance, dot file and directory are ignored).
某些文件被全局gitignore
文件忽略(例如,点文件和目录被忽略)。
回答by Jonathan Leffler
Add the file names - or templates (wild cards) for the file names - to the .gitignore file and add that to the repository:
将文件名 - 或文件名的模板(通配符) - 添加到 .gitignore 文件并将其添加到存储库:
git add .gitignore
git commit -m 'Added .gitignore file'
For example, for my Go repository, I have a .gitignore file containing:
例如,对于我的 Go 存储库,我有一个 .gitignore 文件,其中包含:
*.o
*.a
*.so
*.pl
*.6
*.out
_obj/
_cgo_defun.c
_cgo_export.c
_cgo_export.h
_cgo_gotypes.go
*.cgo1.go
*.cgo2.c
example/example
ifix1/esqlc-cmds.c
I should probably compress the '_cgo_
' names with a wild card; the other '.c' file is generated from a '.ec' file so it does not need to be tracked.
我可能应该_cgo_
用通配符压缩“ ”名称;另一个“.c”文件是从“.ec”文件生成的,因此不需要跟踪。
回答by L S
There are times notifications aren't wanted about either a changed repo file or a new file that needs to be added to the repo. However, adding the name of the file to .gitignore
might not be a good option, either. For example, locally-generated files that other users aren't likely to generate (e.g., files created by an editor) or files of experimental test code might not be appropriate to appear in a .gitignore
file.
有时不需要关于更改的 repo 文件或需要添加到 repo 的新文件的通知。但是,将文件名添加到.gitignore
也可能不是一个好的选择。例如,其他用户不太可能生成的本地生成文件(例如,由编辑器创建的文件)或实验测试代码文件可能不适合出现在.gitignore
文件中。
In those cases, use one of these solutions:
在这些情况下,请使用以下解决方案之一:
If the file is a changed repo file
Use the command:
git update-index --assume-unchanged "$FILE"
To undo this, use the command:
git update-index --no-assume-unchanged "$FILE"
The
update-index
command doesn't work with new files that haven't been added to the repo yet, though.If the file is new and not added to the repo
Add its filename to the repo's
exclude
file:echo "$FILE" >> .git/info/exclude
This also works for changed repo files, but there isn't a specific undo command for it. The
exclude
file would need to be edited and the filename deleted from it. Or other commands could approximate it:ex -s -c"g/^${FILE}\$/d" -cwq .git/info/exclude
Note that this overwrites the existing
exclude
file and if the filename specified contains special characters that could affect the regular expression, the results are unpredictable.This usage of the
exclude
file is recommended by the page "Ignoring files" on GitHub Help.
如果文件是已更改的 repo 文件
使用命令:
git update-index --assume-unchanged "$FILE"
要撤消此操作,请使用以下命令:
git update-index --no-assume-unchanged "$FILE"
但是,该
update-index
命令不适用于尚未添加到存储库中的新文件。如果文件是新文件且未添加到存储库中
将其文件名添加到 repo 的
exclude
文件中:echo "$FILE" >> .git/info/exclude
这也适用于更改的 repo 文件,但没有针对它的特定撤消命令。该
exclude
文件将需要编辑和文件名从文件中删除。或者其他命令可以近似它:ex -s -c"g/^${FILE}\$/d" -cwq .git/info/exclude
请注意,这会覆盖现有
exclude
文件,如果指定的文件名包含可能影响正则表达式的特殊字符,则结果是不可预测的。
回答by kumar
If you are using git-extensions tool in the commit tab just select the files you want to ignore, right click and add them to .gitignore
如果您在提交选项卡中使用 git-extensions 工具,只需选择要忽略的文件,右键单击并将它们添加到 .gitignore