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

发布于 2024-08-09 12:55:08 字数 65 浏览 14 评论 0 原文

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

I am a new user to git and 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 git like there is for svn?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

把时间冻结 2024-08-16 12:55:08

没有特殊的 gitignore 命令。

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

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

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

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

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

There is no special git ignore command.

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

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

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

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.

Run git help gitignore and read the text for the details.

岁吢 2024-08-16 12:55:08

一个非常有用的 gitignore 命令附带太棒了tj/git-extras

以下是一些用法示例:

列出所有当前忽略的模式

git ignore

添加模式

git ignore "*.log"

添加来自 gitignore.io 的模板之一

git ignore-io -a rails

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

A very useful git ignore command comes with the awesome 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

git ignore-io -a rails

git-extras provides many more useful commands. Definitely worth trying out.

困倦 2024-08-16 12:55:08

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

echo .svn/ >> .gitignore

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

echo .svn/ >> .gitignore
绾颜 2024-08-16 12:55:08

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

  • 任何文件夹中的 .gitignore 将忽略该文件夹的文件中指定的文件。可以使用通配符。
  • .git/info/exclude 保存全局忽略模式,类似于 subversions 配置文件中的 global-ignores

You have two ways of ignoring files:

  • .gitignore in any folder will ignore the files as specified in the file for that folder. Using wildcards is possible.
  • .git/info/exclude holds the global ignore pattern, similar to the global-ignores in subversions configuration file.
抹茶夏天i‖ 2024-08-16 12:55:08

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

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.

野侃 2024-08-16 12:55:08

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

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

It's useful to define a complete .gitignore file for your project. The reward is safe use of the convenient --all or -a flag to commands like add and commit.

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

逐鹿 2024-08-16 12:55:08

使用已经提供的答案,您可以使用 别名。将其添加到您的 ~/.gitconfig 文件中:

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

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

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

您同样可以通过替换 ignoregit except 命令> 与上面的 exclude.gitignore 以及 .git/info/exclude

(如果您在阅读此处的答案后还不了解这两个文件之间的区别,请参阅

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

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

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

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

You can likewise create a git exclude command by replacing ignore with exclude and .gitignore with .git/info/exclude in the above.

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

只想待在家 2024-08-16 12:55:08

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

$ sudo apt-get install git-extras

然后你可以使用gitignore命令。

$ git ignore file_name

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

$ sudo apt-get install git-extras

Then you can use the git ignore command.

$ git ignore file_name
静谧 2024-08-16 12:55:08

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

echo /globpattern >> .gitignore

或对于现有文件(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

for names not present in the working copy or repo:

echo /globpattern >> .gitignore

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

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
坠似风落 2024-08-16 12:55:08

使用 Joe Blau 的 gitignore.io

您还可以通过网络界面

https://www.gitignore.io/或者通过安装 CLI 工具,非常简单快捷,只需在终端上输入以下内容:

Linux:
echo "function gi() { curl -L -s https://www.gitignore.io/api/\$@ ;}" >>> 〜/.bashrc &&源 ~/.bashrc

OSX:
echo "function gi() { curl -L -s https://www.gitignore.io/api/\$@ ;}" >>> 〜/.bash_profile && source ~/.bash_profile

然后您只需输入 gi ,然后输入您需要 gitignore 标准的所有平台/环境元素。

示例!
假设您正在开发一个包含 grunt 的节点项目,并且您正在 Linux 上使用 webstorm,那么您可能需要输入:
gi linux,webstorm,node,grunt > .gitignore(创建一个全新的文件)

gi linux,webstorm,node,grunt >>> .gitignore (将新规则附加/添加到现有文件中)

bam,你可以走了

You could also use Joe Blau's gitignore.io

Either through the web interfase https://www.gitignore.io/

Or by installing the CLI tool, it's very easy an fast, just type the following on your terminal:

Linux:
echo "function gi() { curl -L -s https://www.gitignore.io/api/\$@ ;}" >> ~/.bashrc && source ~/.bashrc

OSX:
echo "function gi() { curl -L -s https://www.gitignore.io/api/\$@ ;}" >> ~/.bash_profile && source ~/.bash_profile

And then you can just type gi followd by the all the platform/environment elements you need gitignore criteria for.

Example!
Lets say you're working on a node project that includes grunt and you're using webstorm on linux, then you may want to type:
gi linux,webstorm,node,grunt > .gitignore ( to make a brand new file)
or
gi linux,webstorm,node,grunt >> .gitignore ( to append/add the new rules to an existing file)

bam, you're good to go

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文