Saras Arya
Saras Arya

Reputation: 3112

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

I know there a dozen of posts on SO. Some date back to '12. I am not able to get it work. In a modern world scenario. I have an angularJS project with a structure like

.
├── 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. But not all of bower_components.

So I have a module in app/bower_components/chartist which I want to commit rest I want to ignore.

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

.gitignore~
app/bower_components/

It is including all of the bower_components. Why? How do I include only the chartist folder and leave out the rest? 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. For which I need to track changes. I don't understand how can someone consider a duplicate of that question.

Upvotes: 0

Views: 579

Answers (2)

akshay_rahar
akshay_rahar

Reputation: 1791

create a .gitignore file, so to do that, you just create a filename.txt file and change the extension.

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. 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.

app/bower_components/*

!app/bower_components/chartist

The pattern dir/* says nothing about dir itself; it just excludes everything under 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/).

Upvotes: 0

Martin Svalin
Martin Svalin

Reputation: 2267

Git will continue to track files in the repo, even if they are ignored. 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. 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. This will add the file to the repo, overriding your gitignore. Changes to the file will be tracked going forward.

Alternatively, use your gitignore with the !app/bower_components/chartist line. You won't need to force-add, and it serves as documentation that you are intentionally not ignoring that file.

Upvotes: 1

Related Questions