2
./main
  ../firstDirectory 
    ../file_A.js
    ../file_B.js
    ../file_C.js
    ../file_D.js
    ../file_E.js

I'm trying to create a script that only adds the changes for specific files in some directory, such as file_A, file_C and file_E

In package.json

"scripts": {
  add: "git add <path pattern>"
}

I have tried something like git add ./main/firstDirectory/*.js, but that will add all files. Is there any path pattern that I can specify so git add can know to only add the changes for specific files ?

Update: Although this works:

git add main/firstDirectory/file_A.js main/firstDirectory/file_C.js main/firstDirectory/file_E.js

I'm looking for a dynamic solution(pattern), so I won't have to update the script if consider adding more files in future.

I have tried a lot, and couldn't to come up with an answer. I would appreciate any help.

Max Doung
  • 221
  • 3
  • 14
  • `git add main/firstDirectory/file_A.js main/firstDirectory/file_C.js main/firstDirectory/file_E.js`? – melpomene Apr 21 '18 at 08:47
  • Thanks for the quick response. That works, but I'm looking for something more dynamic, so I won't have to keep updating the script – Max Doung Apr 21 '18 at 08:51

1 Answers1

0

Try

git -C ./main/firstDirectory add '*.js'

Since the -C option does a cd into the right folder, *.js without quotes should work too.

See "“git add” using wildcard is not functioning as I hoped - must I cd into specific directories?".

That will add all the files only if they have changed.

But if you want to keep the other files untracked (meaning never add them in the first place), then you can whitelist the files you want to monitor:

Write in a .gitignore in that folder:

*
!file_A
!file_C
!file_D
!file_E

That way, even if the other files change, they won't be added by a *.js.

Make sure those files are not tracked:

cd main/firstDirectory
git rm --cached -- *.js
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • I think this will still add all files(file_A, file_B, file_C, file_D, file_E). However, I want to add only file_A, file_C and file_E. – Max Doung Apr 21 '18 at 08:57
  • I put them in a .gitignore file but I keep getting this error: `The following paths are ignored by one of your .gitignore files:` – Max Doung Apr 21 '18 at 09:27
  • Make sure those files were not tracked before – VonC Apr 21 '18 at 09:27
  • It seems like the command is not running correctly. It keeps throwing errors. That's how I have it ` "scripts": { add: "git -C ./main/firstDirectory add *.js" } ` – Max Doung Apr 21 '18 at 09:32
  • For checking if a file ignored, git check-ignorr -v -- afile – VonC Apr 21 '18 at 09:34
  • Yes, it seems like they were tracked it before. How can I make them untracked ? – Max Doung Apr 21 '18 at 09:52
  • Go in the folder and do git rm --cached -- *.js – VonC Apr 21 '18 at 09:58
  • They are not tracked; however, I still keep getting this error: `The following paths are ignored by one of your .gitignore files:` Thanks a lot for being really patience with me – Max Doung Apr 21 '18 at 10:19
  • The check-ignore command I mentioned before will help you determine which rule is involved – VonC Apr 21 '18 at 10:31