git 是否有像 svn 一样的忽略命令?
我是 git 的新用户,我正在开始一个新项目。我有一堆我想忽略的点文件。是否有像 svn 那样的 git 忽略命令?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我是 git 的新用户,我正在开始一个新项目。我有一堆我想忽略的点文件。是否有像 svn 那样的 git 忽略命令?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(10)
没有特殊的 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.一个非常有用的 gitignore 命令附带太棒了tj/git-extras。
以下是一些用法示例:
列出所有当前忽略的模式
添加模式
添加来自 gitignore.io 的模板之一
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
Add a pattern
Add one of the templates from gitignore.io
git-extras provides many more useful commands. Definitely worth trying out.
在 Linux/Unix 上,您可以使用
echo
命令将文件附加到 .gitignore 文件。例如,如果您想忽略所有.svn
文件夹,请从项目的根目录运行以下命令: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:您有两种忽略文件的方法:
.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 theglobal-ignores
in subversions configuration file.在存储库的根目录中创建一个名为 .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.为您的项目定义完整的 .gitignore 文件非常有用。奖励是安全地使用方便的
--all
或-a
标志来执行add
和commit
等命令。另外,考虑为通常被忽略的模式(例如
*~
)定义一个全局 ~/.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 likeadd
andcommit
.Also, consider defining a global ~/.gitignore file for commonly ignored patterns such as
*~
, which covers temporary files created by Emacs.使用已经提供的答案,您可以使用 别名。将其添加到您的 ~/.gitconfig 文件中:
或者从您选择的 (*nix) shell 运行此命令:
您同样可以通过替换
ignore
git 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:Or run this command from the (*nix) shell of your choice:
You can likewise create a
git exclude
command by replacingignore
withexclude
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.)
为此,您必须安装 git-extras。你可以在Ubuntu中使用apt-get安装它,
然后你可以使用gitignore命令。
You have to install git-extras for this. You can install it in Ubuntu using apt-get,
Then you can use the git ignore command.
对于工作副本或存储库中不存在的名称:
或对于现有文件(sh 类型命令行):
for names not present in the working copy or repo:
or for an existing file (sh type command line):
使用 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