gpt4 book ai didi

git - 有没有办法将目录添加到 Git 但排除 .gitignore 中的扩展名?

转载 作者:太空狗 更新时间:2023-10-29 12:56:44 26 4
gpt4 key购买 nike

有没有办法将目录添加到 Git,但排除 .gitgnore 中的扩展名?

例如,当我执行添加目录的命令时,例如

git add console/*

git 响应响应

$ git add console/*
The following paths are ignored by one of your .gitignore files:
console/networkprobing.ppu
console/opsdatamodule.lrs
console/opsdatamodule.o
console/opsdatamodule.ppu
console/rcacsSystemDM.compiled
console/rcacsSystemDM.o
console/rcacsSystemTestProcs.o
console/rcacsSystemTestProcs.ppu
Use -f if you really want to add them.
fatal: no files added

如何让命令自动排除.gitignore 扩展名?是否有一些额外的选项需要传递?

最佳答案

git add console/*

问题是通配符是通过 shell globing 扩展的,而不是直接传递给 git,所以 git-add 实际上是传递给控制台目录中的每个文件[1]。因为 git-add 正在接收目录中的每个文件作为参数,它认为您可能真的在尝试添加一个被忽略的文件,这就是为什么会出现警告并提示使用 -f 如果那是您真正想要做的.

解决方案是不使用 shell globing,而是像这样传递目录。

git add console

类似地,除了被忽略的文件 [2] 之外,将所有内容(包括以 '.' 开头的文件)添加到存储库的正确方法是使用 git add . 而不是 git 添加 *.

[1] 以“.”开头的除外假设您使用的是类似 sh 的 shell,而不是在 Windows cmd.exe 命令提示符下尝试执行此操作。如果您使用的是 Windows 命令提示符,您应该切换到使用来自 msysgit 的 Git-Bash shell 或来自 cygwin 的 shell,具体取决于您安装 git 的方式。
[2] .gitignore 仅用于防止未跟踪的文件被跟踪,如果文件已经被 git 跟踪,那么它将继续正常处理,直到它从 repo 中删除。使用 git rm --cached filename 可以在不影响实际文件的情况下完成此操作。

关于git - 有没有办法将目录添加到 Git 但排除 .gitignore 中的扩展名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4754396/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com