【问题标题】:Add files to .gitignore directly from git shell直接从 git shell 将文件添加到 .gitignore
【发布时间】:2017-09-23 19:31:22
【问题描述】:

我正在尝试直接从 git shell 将特定文件添加到 .gitignore。要忽略的文件是token.mat

如果我输入:

touch .gitignore

然后用文本编辑器手动打开.gitignore并添加

token.mat

保存并关闭,它可以工作,git 忽略它(当我在 git shell 中输入 git status 时,我不再看到 token.mat)

但是,如果我在 git shell 中输入:

touch .gitignore
echo 'token.mat' >> .gitignore

然后 git 不会忽略 token.mat,当我输入 git status 时,我仍然可以看到它。但是,当我使用文本编辑打开 .gitignore 时,我看到列出了 token.mat,它看起来与我手动添加时完全相同。

有谁知道这是为什么,或者我如何直接从 git shell 将文件添加到 .gitignore?

提前感谢您的帮助!

其他细节:

  • Windows 7 专业版,服务包 1
  • git 版本:2.5.3.windows.1
  • 文本编辑器:记事本++

【问题讨论】:

  • 文本编辑器是否处理向 git 项目添加/处理文件索引?

标签: git echo gitignore git-shell


【解决方案1】:

确保您的回声没有添加尾随空格:

echo 'token.mat'>>.gitignore

这样,.gitignore 应该可以工作。

另外,2.5 是古老的:解压缩 the latest Git,如 PortableGit-2.14.1-64-bit.7z.exe,任何你想要的地方,add it to your %PATH%,然后再次检查。

【讨论】:

  • 感谢 VonC!没有尾随空格,但升级到 Git 2.14 就可以了。另外一点,对于尝试此解决方案的其他人,我还通过升级从 Git Shell 更改为 Git Bash。
【解决方案2】:

接受的答案是完全正确的,但在日常情况下会不完整,因为它只向 .gitignore 添加一个文件,向您的 .gitignore 添加多个文件

使用以下命令:

echo -e 'file1 \n file2 \n file3 \n' >> .gitignore

您可以在没有touch 的情况下使用上述命令,这将自动创建一个.gitignore 并添加所有文件。确保包含-e>>

这里-e 用作标志,因此换行符\n 被正确解释,>> 将内容附加到.gitignore 文件中。

如果您需要覆盖已经存在的.gitignore,请使用> 而不是>>

您可以了解更多关于echoby following this link.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-07-08
    • 1970-01-01
    • 2012-04-02
    • 2013-01-14
    • 2019-04-16
    • 1970-01-01
    • 2022-08-15
    • 2015-07-08
    相关资源
    最近更新 更多