如何在Windows上使用msysgit忽略Git中的目录或文件夹?
当前回答
我认为问题是你的工作树是这样的:
a-cache/foo
a-cache/index.html
b-cache/bar
b-cache/foo
b-cache/index.html
.gitignore
…用你描述的.gitignore。这将为您提供git状态输出,如:
$ git status
# On branch master
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# .gitignore
# a-cache/
# b-cache/
…如果index.html文件尚未添加到存储库中。(Git发现缓存目录中有未忽略的文件,但它只报告目录。)要解决此问题,请确保您已添加并提交了index.html文件:
git add *cache/index.html
git commit -m "Adding index.html files to the cache directories"
…然后您的git状态将如下所示:
$ git status
# On branch master
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# .gitignore
nothing added to commit but untracked files present (use "git add" to track)
(很明显,你也希望提交.gitignore。我只是懒得处理这个测试用例。)
其他回答
默认情况下,当文件名为.gitignore.txt时,Windows资源管理器将显示.gitignore。
Git不会使用.gitignore.txt
您不能将文件重命名为.gitignore,因为Windows资源管理器认为它是一个没有名称的gitignore类型的文件。
非命令行解决方案:
You can rename a file to ".gitignore.", and it will create ".gitignore"
要指示Git忽略某些文件或文件夹,必须创建.gitignore文件。
但在Windows资源管理器中,您必须提供文件的名称。您无法创建仅具有扩展名的文件。诀窍是创建一个空文本文件,然后转到命令提示符,将文件名更改为.gitignore:
ren "New Text Document.txt" .gitignore
现在用您最喜欢的文本编辑器打开文件,并添加您希望忽略的文件/文件夹名称。也可以使用通配符,如*.txt。
我认为问题是你的工作树是这样的:
a-cache/foo
a-cache/index.html
b-cache/bar
b-cache/foo
b-cache/index.html
.gitignore
…用你描述的.gitignore。这将为您提供git状态输出,如:
$ git status
# On branch master
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# .gitignore
# a-cache/
# b-cache/
…如果index.html文件尚未添加到存储库中。(Git发现缓存目录中有未忽略的文件,但它只报告目录。)要解决此问题,请确保您已添加并提交了index.html文件:
git add *cache/index.html
git commit -m "Adding index.html files to the cache directories"
…然后您的git状态将如下所示:
$ git status
# On branch master
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# .gitignore
nothing added to commit but untracked files present (use "git add" to track)
(很明显,你也希望提交.gitignore。我只是懒得处理这个测试用例。)
在Windows和Mac上,如果要忽略当前目录中名为Flower_Data_folder的文件夹,可以执行以下操作:
echo Flower_Data_Folder >> .gitignore
如果是名为data.txt的文件:
echo data.txt >> .gitignore
如果是类似“Data/passwords.txt”的路径
echo "Data/passwords.txt" >> .gitignore.
这对一些人来说可能非常明显,但我确实从其他答案中理解了这一点。
在目录中创建.gitignore文件本身没有任何作用。您必须将.gitignore作为文本文件打开,并将希望它忽略的文件/目录写在各自的行上。
所以cd到Git存储库目录
touch .gitignore
nano .gitignore
然后写下要忽略的文件和/或目录的名称及其扩展名(如果相关)。
此外,.gitignore在某些操作系统(例如Mac)上是一个隐藏文件,因此您需要ls-a来查看它,而不仅仅是ls。
推荐文章
- 当我试图推到原点时,为什么Git告诉我“没有这样的远程‘原点’”?
- 如何从远程分支中挑选?
- 如何查看一个分支中的哪些提交不在另一个分支中?
- 如何取消在github上的拉请求?
- HEAD和master的区别
- GIT克隆在windows中跨本地文件系统回购
- RPC失败;卷度传输已关闭,剩余未完成的读取数据
- 我应该在.gitignore文件中添加Django迁移文件吗?
- 错误:您对以下文件的本地更改将被签出覆盖
- Git rebase—即使所有合并冲突都已解决,仍然会继续报错
- 在Git中,我如何知道我的当前版本是什么?
- 如何运行一个PowerShell脚本而不显示窗口?
- PowerShell:仅为单个命令设置环境变量
- 跟踪所有远程git分支作为本地分支
- 自定义SSH端口上的Git