0

I'm trying to create a cronjob to backup all my Jenkins configuration in git. The only thing I need to be tracked in my repository is the config.xml in every Jenkins project. The file system is structured like ../jenkins/jobs/[PROJECTNAME]/config.xml. The .git is located in jobs.

I don't want to explicitly ignore all other folder/files, but allow only the config.xml in that location. I came up with the following working solution:

*

!.gitignore
!config.xml
!*/

*/workspace
*/modules
/config-history
*/config-history
*/*/config-history
*/*/*/config-history
*/*/*/*/config-history
*/*/*/*/*/config-history
*/*/*/*/*/*/config-history
*/*/*/*/*/*/*/config-history 

Unfortunate there are many projects and the git add . takes a while. Ignore workspace, the directly with most files in, didn't add any benefit.

Btw. the intuitive and short way didn't work at all:

*
!.gitignore
!*/config.xml

Any ideas to either optimize my .gitignore or my git add without listing all files in either case?

mheinzerling
  • 1,035
  • 1
  • 10
  • 31

2 Answers2

2

since this is but an automated task, why don't you just simply add only the files you need? Replace

git add .

with

git add config.xml

and git will not have to traverse the whole project tree.

If you want to show the status of your repo, without the load of untracked files, use

git status -uno

EDIT: Of course you have to be in the directory with the config.xml you want to add; if you want to add all config.xmls in your current directory's subdirectories, use

git add */config.xml
Marcus Müller
  • 34,677
  • 4
  • 53
  • 94
  • I quickly tried your approuch on a sample repo, but this is not working on subdirectories? There are added new projects at any time and of course I want to backup also new config.xml. Keep in mind that the configs are located in /[projectname]/config.xml in relation to git. – mheinzerling Feb 10 '15 at 17:17
  • `git add ` is unambigous. The only reason that *you* need .gitignore is that, instead of adding the file you actually want to add, you add the whole directory containing the file. Thus: Are you still doing `git add .`? You really should not. – Marcus Müller Feb 10 '15 at 17:19
  • I don't get it. See my example: `>mkdir gittest >cd gittest \gittest>git init . Initialized empty Git repository in C:/Users/User/gittest/.git/ \gittest>echo "xx" > config.xml \gittest>mkdir sub \gittest>cd sub \gittest\sub>echo "xx2" > config.xml \gittest\sub>echo "xx4" > other.xml \gittest\sub>cd .. \gittest>git add -n . add 'config.xml' add 'sub/config.xml' add 'sub/other.xml' \gittest>git add -n config.xml add 'config.xml'` (Copy it somewhery to read it :)) – mheinzerling Feb 10 '15 at 17:34
  • This was for test purpose only. The second was the important. But now I have it: `>git add -n */config.xml` – mheinzerling Feb 10 '15 at 17:37
  • Please update you anwser to really match my question. I excepted and upvoted it already, as it was the right direction. – mheinzerling Feb 10 '15 at 17:40
  • It matches your question. I was presuming you understood the basic path logic of git :) – Marcus Müller Feb 10 '15 at 17:43
1

Well, you could at least shorten your .gitignore by using the double-asterisk:

# other stuff here
**/config-history

It will match config-history in any path.

haliphax
  • 393
  • 1
  • 9
  • Thanks for the hind, but I can remember that I had trouble with this while experimenting. But I will retest this again. – mheinzerling Feb 10 '15 at 17:19