mazunki
mazunki

Reputation: 732

Is there a way to gitignore all current extra files?

I have a set of files in a git repository which I want to ignore.

I know the simplest solution: Add them all to .gitignore manually.

I do not want to ignore all further files, so path/* is not an option. If I somehow could use path/*, let git parse it, and then save the current "extra files", that would be perfect, but as far as I'm concerned there is no easy way to do that.

The alternative would be to make a script which greps the filenames after git status' untracked files, and sends the output to .gitignore, which might be what I will end up doing.

Any tips are appreciated.

Related, but not my main question: Is there a reason why git doesn't include a git ignore command?

Upvotes: 0

Views: 69

Answers (3)

Daemon Painter
Daemon Painter

Reputation: 3520

Finally, in alternative you could exclude everything and just force add what you know you want.

path/*               # exclude all
!path/keep_this      # but not this
!path/keep_that      # and that

Upvotes: -1

torek
torek

Reputation: 490098

[An] alternative would be to make a script which greps the filenames after git status' untracked files, and sends the output to .gitignore, which might be what I will end up doing.

For writing scripts, always try to use plumbing commands.

With git status there is the --porcelain option1 to turn it into a plumbing command. Consider also using git ls-files --others (-o for short) but note that you would want to add the --exclude-standard option.

Related, but not my main question: Is there a reason why git doesn't include a git ignore command?

Define what, precisely, it would do; write one; and submit it and see if you can get it accepted. :-)


1As always, I express my own bafflement as to why this option is not spelled --plumbing.

Upvotes: 2

mazunki
mazunki

Reputation: 732

Dirty script which adds all untracked files into gitignore:

git status -s | awk '{ if ($1 == "??") print $2 }' >> .gitignore

In short format of git status, files are printed per line, with no fluff. The first column tells the status of the files listed. Specifically, untracked files are ??. Read up on git status --help for other combinations.

Matching on first column, and printing second column with awk, we append each line into .gitignore.

There might be a better or cleaner solution, but this works.

Upvotes: 0

Related Questions