简体   繁体   English

直接从 git shell 添加文件到.gitignore

[英]Add files to .gitignore directly from git shell

I'm trying to add a specific file to.gitignore directly from git shell.我正在尝试将特定文件直接从 git shell 添加到 .gitignore。 File to ignore is token.mat要忽略的文件是token.mat

If I type:如果我输入:

 touch.gitignore

and then manually open.gitignore with a text editor and add然后用文本编辑器手动打开.gitignore 并添加

token.mat

save and close, it works, git ignores it (when I type git status at the git shell I no longer see token.mat)保存并关闭,它可以工作,git 忽略它(当我在 git Z2591C98B710119FE6E948 中输入 git 状态时)

However at the git shell if I type:但是在 git shell 如果我输入:

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

then git does not ignore token.mat, when I type git status I still see it.然后 git 不会忽略 token.mat,当我输入 git status 我仍然看到它。 However when I open.gitignore with a text edit I see token.mat listed and it looks exactly the same as when I add it manually.但是,当我使用文本编辑打开.gitignore 时,我看到列出了 token.mat,它看起来与我手动添加时完全相同。

Does anyone know why this is, or how I can add files to.gitignore directly from the git shell?有谁知道这是为什么,或者我如何直接从 git shell 将文件添加到 .gitignore?

Thanks for any help in advance感谢您提前提供任何帮助

Other details:其他详情:

  • Windows 7 Professional, service pack 1 Windows 7 专业版,服务包 1
  • git version: 2.5.3.windows.1 git 版本:2.5.3.windows.1
  • text editor: Notepad ++文本编辑器:记事本++

Make sure your echo did not add a trailing space:确保您的回声没有添加尾随空格:

 echo 'token.mat'>>.gitignore

That way, the .gitignore should work.这样, .gitignore应该可以工作。

>> is for appending to the .gitignore file, while a single > would have overwritten it completely. >>用于附加.gitignore文件,而单个>将完全覆盖它。

Also, 2.5 is ancient: unzip the latest Git , as in PortableGit-2.14.1-64-bit.7z.exe , anywhere you want, add it to your %PATH% , and check again.此外,2.5 是古老的:解压缩最新的 Git ,如PortableGit-2.14.1-64-bit.7z.exe ,在您想要的任何位置,将其添加到您的%PATH% ,然后再次检查。

The accepted answer is totally correct, but would be incomplete in day to day scenarios as it only add one file to .gitignore , to add more than one files to your .gitignore接受的答案是完全正确的,但在日常场景中是不完整的,因为它只向.gitignore添加一个文件,向您的.gitignore添加多个文件

Use the following command:使用以下命令:

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

You can use the above command without touch this will automatically create a .gitignore and add all the files.您可以使用上述命令而无需touch ,这将自动创建一个.gitignore并添加所有文件。 Make sure that you include -e and >> .确保包含-e>>

Here -e serves as the flag so the newline character \n is interpreted properly and >> appends the content in the .gitignore file.这里-e用作标志,因此换行符\n被正确解释, >>将内容附加到.gitignore文件中。

If you need to override an already existing .gitignore use > instead of >>如果您需要覆盖已经存在的.gitignore使用>而不是>>

You can learn more about echo by following this link.您可以通过此链接了解有关echo的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM