如何在 git 中使用 gitignore 命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12501324/
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
How to use gitignore command in git
提问by user1559230
I'm working first time on git. I have pushed my branch on github and it pushed all the library and documents into the github. Now what can I do and how can I use gitignore command to avoid the same mistake again.
我是第一次在 git 上工作。我已经在 github 上推送了我的分支,它把所有的库和文档都推送到了 github 中。现在我能做什么以及如何使用 gitignore 命令来避免再次出现同样的错误。
回答by u19964
So based on what you said, these files are libraries/documentation you don't want to delete but also don't want to push to github
. Let say you have your project in folder your_project
and a doc directory: your_project/doc
.
因此,根据您所说的,这些文件是您不想删除但也不想推送到的库/文档github
。假设您的项目位于文件夹your_project
和 doc 目录中:your_project/doc
.
- Remove it from the project directory (without actually deleting it):
git rm --cached doc/*
- If you don't already have a
.gitignore
, you can make one right inside of your project folder:project/.gitignore
. - Put
doc/*
in the .gitignore - Stage the file to commit:
git add project/.gitignore
- Commit:
git commit -m "message"
. - Push your change to
github
.
- 从项目目录中删除它(实际上并没有删除它):
git rm --cached doc/*
- 如果你还没有
.gitignore
,你可以在你的项目文件夹中创建一个:project/.gitignore
。 - 放入
doc/*
.gitignore - 暂存要提交的文件:
git add project/.gitignore
- 承诺:
git commit -m "message"
。 - 将您的更改推送到
github
.
回答by Serdar De?irmenci
If you dont have a .gitignore file, first use:
如果您没有 .gitignore 文件,请先使用:
touch .gitignore
then this command to add lines in your gitignore file:
然后这个命令在你的 gitignore 文件中添加行:
echo 'application/cache' >> .gitignore
Be careful about new lines
小心新行
回答by Adam Dymitruk
git ignore is a convention in git. Setting a file by the name of .gitignore
will ignore the files in that directory and deeper directories that match the
patterns that the file contains. The most common use is just to have one file
like this at the top level. But you can add others deeper in your directory
structure to ignore even more patterns or stop ignoring them for that directory
and subsequently deeper ones.
git ignore 是 git 中的约定。通过名称设置文件.gitignore
将忽略该目录中的文件以及与该文件包含的模式匹配的更深层次的目录。最常见的用途就是在顶层有一个这样的文件。但是您可以在目录结构中添加更深层次的其他内容以忽略更多模式或停止忽略该目录以及随后更深层次的模式。
Likewise, you can "unignore" certain files in a deeper structure or a specific
subset (ie, you ignore *.log but want to still track important.log) by
specifying patterns beginning with !
. eg:
同样,您可以通过指定以!
. 例如:
*.log !important.log
will ignore all log files but will track files named important.log
将忽略所有日志文件,但会跟踪名为 important.log 的文件
If you are tracking files you meant to ignore, delete them, add the pattern to you .gitignore file and add all the changes
如果您正在跟踪要忽略的文件,请删除它们,将模式添加到您的 .gitignore 文件并添加所有更改
# delete files that should be ignored, or untrack them with
# git rm --cached <file list or pattern>
# stage all the changes git commit
git add -A
from now on your repository will not have them tracked.
从现在开始,您的存储库将不会跟踪它们。
If you would like to clean up your history, you can
如果你想清理你的历史,你可以
# if you want to correct the last 10 commits
git rebase -i --preserve-merges HEAD~10
then mark each commit with e
or edit
. Save the plan. Now git will replay
your history stopping at each commit you marked with e. Here you delete the
files you don't want, git add -A
and then git rebase --continue
until you
are done. Your history will be clean. Make sure you tell you coworkers as you
will have to force push and they will have to rebase what they didn't push yet.
然后用e
或标记每个提交edit
。保存计划。现在 git 将重播您的历史记录,在您标记为 e 的每个提交处停止。在这里你删除你不想要的文件,git add -A
然后git rebase --continue
直到你完成。你的历史将是干净的。确保你告诉你的同事,因为你将不得不强制推送,而他们将不得不重新调整他们尚未推送的内容。
回答by LonelyWebCrawler
There is a file in your git root directory named .gitignore
. It's a file, not a command. You just need to insert the names of the files that you want to ignore, and they will automatically be ignored. For example, if you wanted to ignore all emacs autosave files, which end in ~
, then you could add this line:
在您的 git 根目录中有一个名为.gitignore
. 这是一个文件,而不是一个命令。您只需要插入要忽略的文件的名称,它们将被自动忽略。例如,如果您想忽略所有以 结尾的 emacs 自动保存文件~
,那么您可以添加以下行:
*~
If you want to remove the unwanted files from your branch, you can use git add -A
, which "removes files that are no longer in the working tree".
如果要从分支中删除不需要的文件,可以使用git add -A
,它“删除不再存在于工作树中的文件”。
Note: What I called the "git root directory" is simply the directory in which you used git init
for the first time. It is also where you can find the .git
directory.
注意:我所说的“git根目录”只是你git init
第一次使用的目录。它也是您可以找到.git
目录的地方。
回答by karthik vishnu kumar
If you don't have a .gitignore file. You can create a new one by
如果您没有 .gitignore 文件。您可以创建一个新的
touch .gitignore
And you can exclude a folder by entering the below command in the .gitignore file
您可以通过在 .gitignore 文件中输入以下命令来排除文件夹
/folderName
push this file into your git repository so that when a new person clone your project he don't have to add the same again
将此文件推送到您的 git 存储库中,以便当一个新人克隆您的项目时,他不必再次添加相同的内容
回答by Afaq Ahmed Khan
There are several ways to use gitignore git
gitignore git有几种使用方法
- specifying by the specific filename. for example, to ignore a file
called readme.txt, just need to write readme.txt in .gitignore file. - you can also write the name of the file extension. For example, to
ignore all .txt files, write *.txt. - you can also ignore a whole folder. for example you want to ignore
folder named test. Then just write test/ in the file.
- 通过特定文件名指定。比如忽略一个
叫做 readme.txt的文件,只需要在 .gitignore 文件中写入 readme.txt 即可。 - 你也可以写文件扩展名。例如,要
忽略所有 .txt 文件,请写入 *.txt。 - 您也可以忽略整个文件夹。例如,您想忽略
名为 test 的文件夹。然后只需在文件中写入 test/ 。
just create a .gitignore file and write in whatever you want to ignore a sample gitignore file would be:
只需创建一个 .gitignore 文件并写入任何您想忽略的示例 gitignore 文件:
# NPM packages folder.
node_modules
# Build files
dist/
# lock files
yarn.lock
package-lock.json
# Logs
logs
*.log
npm-debug.log*
# node-waf configuration
.lock-wscript
# Optional npm cache directory
.npm
# Optional REPL history
.node_repl_history
# Jest Coverage
coverage
.history/
You can find more on git documentation gitignore
您可以在git 文档 gitignore上找到更多信息
回答by j2emanue
on my mac i found this file .gitignore_global
..it was in my home directory hidden so do a ls -altr
to see it.
在我的 Mac 上,我找到了这个文件。它.gitignore_global
隐藏在我的主目录中,所以请ls -altr
查看它。
I added eclipse files i wanted git to ignore. the contents looks like this:
我添加了我想让 git 忽略的 eclipse 文件。内容如下所示:
*~
.DS_Store
.project
.settings
.classpath
.metadata