0

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.

Community
  • 1
  • 1
Saras Arya
  • 3,022
  • 8
  • 41
  • 71
  • 1
    try to remove !app/bower_components/, I have not the same structure as you, but for me the pattern from provided link works fine: 1. maindir/* 2. !maindir/subdir1 After adding this to .itignore, the command git add --all ads only the subdir1, even if there are more subdirs and files in the maindir directory – Zavael Mar 16 '16 at 08:19
  • @Zavael can you write that as an answer so that I can accept it – Saras Arya Mar 16 '16 at 08:29
  • Possible duplicate of [Stop tracking and ignore changes to a file in Git](http://stackoverflow.com/questions/936249/stop-tracking-and-ignore-changes-to-a-file-in-git) – 1615903 Mar 17 '16 at 05:40
  • It's not even a close duplicate of [that](http://stackoverflow.com/questions/936249/stop-tracking-and-ignore-changes-to-a-file-in-git) – Saras Arya Mar 17 '16 at 12:36

2 Answers2

1

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.

Martin Svalin
  • 2,227
  • 1
  • 17
  • 23
  • See http://stackoverflow.com/questions/936249/stop-tracking-and-ignore-changes-to-a-file-in-git for how to stop tracking `app/bower_components`. (Or just `git rm --cached app/bower_components`). – Martin Svalin Mar 16 '16 at 08:43
0

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

akshay_rahar
  • 1,661
  • 2
  • 18
  • 20