简体   繁体   English

如何忽略子目录在GIT中的提交

[英]How to ignore a sub-directory from being committed in GIT

I know there a dozen of posts on SO. 我知道在SO上有很多帖子。 Some date back to '12. 有些可以追溯到'12。 I am not able to get it work. 我无法使其正常工作。 In a modern world scenario. 在现代世界中。 I have an angularJS project with a structure like 我有一个angularJS项目,其结构如下

.
├── app
│   ├── app.js
│   ├── assets
│   ├── bower_components
│   ├── controllers
│   ├── index.html
│   ├── services
│   └── views
├── bower.json
├── e2e
│   ├── pages
│   ├── protractor.conf.js
│   └── scenarios
├── karma.conf.js
├── LICENSE
├── package.json
└── README.md
|__ .gitignore

In my bower_components, I have edited a module to suit my needs and I need to commit it so that changes are reflected for everyone. 在我的bower_components中,我已经编辑了一个模块以满足自己的需要,我需要提交它,以便每个人都能看到更改。 But not all of bower_components. 但不是所有的bower_components。

So I have a module in app/bower_components/chartist which I want to commit rest I want to ignore. 所以我在app / bower_components / chartist中有一个模块,想要提交其余部分,而忽略。

From here I tried this 这里我尝试了这个

logs/*
!.gitkeep
node_modules/

!app/bower_components/

app/bower_components/*
!app/bower_components/chartist

tmp
.DS_Store
.idea
app/jspm_packages

It doesn't work. 没用 In my git status, I see this as output 在我的git状态下,我将此视为输出

.gitignore~
app/bower_components/

It is including all of the bower_components. 它包括所有bower_components。 Why? 为什么? How do I include only the chartist folder and leave out the rest? 如何只包含Chartist文件夹,而忽略其余的文件夹? Rules are exclusion are too damn confusing. 排除规则太令人困惑了。 Can anyone simplify it for me? 谁能为我简化一下?

EDIT : My question is asking how to add a folder in .gitignore but leaves a subfolder inside it. 编辑 :我的问题是询问如何在.gitignore中添加文件夹,但在其中保留一个子文件夹。 For which I need to track changes. 为此,我需要跟踪更改。 I don't understand how can someone consider a duplicate of that question. 我不明白有人会怎么考虑那个问题的重复。

Git will continue to track files in the repo, even if they are ignored. 即使忽略,Git也会继续跟踪存储库中的文件。 If all of app/bower_components were checked in when you added the dir to .gitignore, git will still track changes to any files in there already in the repo. 如果在将目录添加到.gitignore时已签入所有app/bower_components ,则git仍会跟踪对存储库中已经存在的任何文件的更改。 If you create a new file in there, it will be ignored. 如果在其中创建新文件,它将被忽略。

If you only want one file in there tracked, remove the rest from the repo. 如果只想跟踪其中的一个文件,请从存储库中删除其余文件。 Now you can gitignore the directory, and then git add -f app/bower_components/chartist . 现在,您可以gitignore目录,然后git add -f app/bower_components/chartist This will add the file to the repo, overriding your gitignore. 这会将文件添加到仓库中,覆盖您的gitignore。 Changes to the file will be tracked going forward. 以后将跟踪对文件的更改。

Alternatively, use your gitignore with the !app/bower_components/chartist line. 或者,将您的gitignore与!app/bower_components/chartist行一起使用。 You won't need to force-add, and it serves as documentation that you are intentionally not ignoring that file. 您无需强制添加,它可以作为您有意不忽略该文件的文档。

create a .gitignore file, so to do that, you just create a filename.txt file and change the extension. 创建一个.gitignore文件,因此,只需创建一个filename.txt文件并更改扩展名即可。

Then change the name of that file through this command:- 然后通过以下命令更改该文件的名称:

rename filename.txt .gitignore

Then you can open the file and write all the files you don´t want to add on the repository. 然后,您可以打开文件并将所有不想添加的文件写入存储库中。

app/bower_components/

The pattern dir/ excludes a directory named dir and (implicitly) everything under it. 模式dir /排除了一个名为dir的目录,并暗含了该目录下的所有内容。 With dir/, Git will never look at anything under dir, and thus will never apply any of the “un-exclude” patterns to anything under dir. 使用dir /,Git将永远不会查看dir下的任何内容,因此绝不会将任何“非排除”模式应用于dir下的任何内容。

app/bower_components/*

!app/bower_components/chartist

The pattern dir/* says nothing about dir itself; 模式dir / *不说明dir本身; it just excludes everything under dir. 它只是排除dir下的所有内容。 With dir/*, Git will process the direct contents of dir, giving other patterns a chance to “un-exclude” some bit of the content (!dir/sub/). 使用dir / *,Git将处理dir的直接内容,使其他模式有机会“取消排除”部分内容(!dir / sub /)。

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

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