是否有像 svn 一样的 git 忽略命令?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1677121/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 07:18:25  来源:igfitidea点击:

Is there an ignore command for git like there is for svn?

gitgitignoreignore

提问by Chris J

I am a new user to gitand I am starting a new project. I have a bunch of dot files that I would like to ignore. Is there an ignore command for gitlike there is for svn?

我是新用户git,我正在开始一个新项目。我有一堆我想忽略的点文件。是否有git像for一样的 ignore 命令svn

回答by ndim

There is no special git ignorecommand.

没有特别的git ignore命令。

Edit a .gitignorefile located in the appropriate place within the working copy. You should then add this .gitignoreand commit it. Everyone who clones that repo will than have those files ignored.

编辑.gitignore位于工作副本中适当位置的文件。然后你应该添加它.gitignore并提交它。每个克隆该 repo 的人都会忽略这些文件。

Note that only file names starting with /will be relative to the directory .gitignoreresides in. Everything else will match files in whatever subdirectory.

请注意,只有以 开头的文件名将与所在/目录相关.gitignore。其他所有内容都将匹配任何子目录中的文件。

You can also edit .git/info/excludeto ignore specific files just in that one working copy. The .git/info/excludefile will not be committed, and will thus only apply locally in this one working copy.

您还可以编辑.git/info/exclude以忽略该工作副本中的特定文件。该.git/info/exclude文件不会被提交,因此只会在这个工作副本中本地应用。

You can also set up a global file with patterns to ignore with git config --global core.excludesfile. This will locally apply to all git working copies on the same user's account.

您还可以设置一个全局文件,其中包含要忽略的模式git config --global core.excludesfile。这将在本地应用于同一用户帐户上的所有 git 工作副本。

Run git help gitignoreand read the text for the details.

运行git help gitignore并阅读文本以了解详细信息。

回答by Nicolai Fr?hlich

A very useful git ignorecommand comes with the awesome tj/git-extras.

一个非常有用的git ignore命令带有很棒的tj/git-extras

Here are a few usage examples:

以下是一些使用示例:

List all currently ignored patterns

列出所有当前忽略的模式

git ignore

Add a pattern

添加图案

git ignore "*.log"

Add one of the templates from gitignore.io

gitignore.io添加模板之一

git ignore-io -a rails

git-extrasprovides many more useful commands. Definitely worth trying out.

git-extras提供了更多有用的命令。绝对值得一试。

回答by pfrenssen

On Linux/Unix, you can append files to the .gitignore file with the echocommand. For example if you want to ignore all .svnfolders, run this from the root of the project:

在 Linux/Unix 上,您可以使用以下echo命令将文件附加到 .gitignore 文件。例如,如果您想忽略所有.svn文件夹,请从项目的根目录运行:

echo .svn/ >> .gitignore

回答by bluebrother

You have two ways of ignoring files:

您有两种忽略文件的方法:

  • .gitignorein any folder will ignore the files as specified in the file for that folder. Using wildcards is possible.
  • .git/info/excludeholds the global ignore pattern, similar to the global-ignoresin subversions configuration file.
  • .gitignore在任何文件夹中将忽略该文件夹的文件中指定的文件。可以使用通配符。
  • .git/info/exclude持有全局忽略模式,类似于global-ignoressubversions 配置文件。

回答by Nesar

I hope it's not too late.

我希望现在还不算太晚。

If you are on Windows you can just do the following to create a .gitignorefile

如果您使用的是 Windows,您只需执行以下操作即可创建.gitignore文件

echo name_of_the_file_you_want_to_ignore.extension > .gitignore

In order to edit .gitignoreyou can run

为了编辑.gitignore你可以运行

notepad .gitignore

回答by Bruno Reis

Create a file named .gitignore on the root of your repository. In this file you put the relative path to each file you wish to ignore in a single line. You can use the *wildcard.

在存储库的根目录上创建一个名为 .gitignore 的文件。在此文件中,您将希望忽略的每个文件的相对路径放在一行中。您可以使用*通配符。

回答by daemonexmachina

Using the answers already provided, you can roll your own git ignorecommand using an alias. Either add this to your ~/.gitconfig file:

使用已经提供的答案,您可以git ignore使用alias滚动您自己的命令。将此添加到您的 ~/.gitconfig 文件中:

ignore = !sh -c 'echo  >> .gitignore' -

Or run this command from the (*nix) shell of your choice:

或者从您选择的 (*nix) shell 运行此命令:

git config --global alias.ignore '!sh -c "echo  >> .gitignore" -'

You can likewise create a git excludecommand by replacing ignorewith excludeand .gitignorewith .git/info/excludein the above.

您同样可以git exclude通过替换上面的ignorewithexclude.gitignorewith.git/info/exclude来创建命令。

(If you don't already understand the difference between these two files having read the answers here, see this question.)

(如果您在阅读此处的答案后还不明白这两个文件之间的区别,请参阅此问题。)

回答by nrbray

for names not present in the working copy or repo:

对于工作副本或仓库中不存在的名称:

echo /globpattern >> .gitignore

or for an existing file (sh type command line):

或对于现有文件(sh 类型命令行):

echo /$(ls -1 file) >> .gitignore   # I use tab completion to select the file to be ignored  
git rm -r --cached file             # if already checked in, deletes it on next commit

回答by Vidura Mudalige

You have to install git-extras for this. You can install it in Ubuntu using apt-get,

您必须为此安装 git-extras。您可以使用 apt-get 在 Ubuntu 中安装它,

$ sudo apt-get install git-extras

Then you can use the git ignore command.

然后你可以使用 git ignore 命令。

$ git ignore file_name

回答by seh

It's useful to define a complete .gitignorefile for your project. The reward is safe use of the convenient --allor -aflag to commands like addand commit.

为您的项目定义一个完整的.gitignore文件很有用。奖励是安全地使用方便--all-a标志来命令addcommit

Also, consider defining a global ~/.gitignorefile for commonly ignored patterns such as *~ , which covers temporary files created by Emacs.

此外,考虑为通常被忽略的模式定义一个全局的~/.gitignore文件,例如*~ ,它涵盖了由 Emacs 创建的临时文件。