77

I am using an ORM which generates large amounts of files from a CLI. Is there an easy way to run the svn add on all files within a directory which appear as ? when I run svn status?

Edit These files exist in a directory tree so adding * for one directory will not work.

Melebius
  • 6,183
  • 4
  • 39
  • 52
Brian
  • 781
  • 1
  • 5
  • 5
  • @your edit: So then provide multiple paths to add like: `svn add dir1/* dir2/* dir3/*` or as many have mentioned `grep` the ouput of `svn stat` from the root and pipe it to `cut` or `awk` and then to add. – prodigitalson Jan 22 '10 at 22:08
  • If you are on Windows you can always use TortoiseSVN to make life easier. – Brian Ogden Sep 23 '13 at 22:01

15 Answers15

161

This will add all unknown (except ignored) files under the specified directory tree:

svn add --force path/to/dir

This will add all unknown (except ignored) files in the current directory and below:

svn add --force .
janos
  • 120,954
  • 29
  • 226
  • 236
70

For reference, these are very similar questions.

These seem to work the best for me. They also work with spaces, and don't re-add ignored files. I didn't see them listed on any of the other answers I saw.

adding:

svn st | grep ^? | sed 's/?    //' | xargs svn add

removing:

svn st | grep ^! | sed 's/!    //' | xargs svn rm

Edit: It's important to NOT use "add *" if you want to keep your ignored files, otherwise everything that was ignored will be re-added.

Community
  • 1
  • 1
phazei
  • 5,323
  • 5
  • 42
  • 46
  • this will work well all until you encounter a filename with space? (similar problem with many of the proposed solutions based on svn stat) – tobixen Sep 03 '13 at 08:19
  • 1
    svn st | grep ^? | sed 's/? *//' | xargs -d'\n' svn add # <- works also for filenames with space – tobixen Sep 03 '13 at 08:23
  • On MacOS X, this seems to break for paths with spaces in them. Sadly the `xargs` that comes with MacOS does not seem to accept the `-d` flag. – amacleod Mar 03 '14 at 21:15
  • I've created a script for that: https://gist.github.com/nestserau/ab7d48e857811e1ef808 – Aleks N. Oct 31 '14 at 08:56
  • I use this method, because it is the best. even though I commented on another person's answer, which was very similar. – Grapho Mar 17 '15 at 14:39
  • this doesn't get "@2x" and "@3x" files, which need to have a final @ added after the file name. You can add a sed expression to your sed using the -e argument as follows: -e 's/$/@/' to add a @ on every line. You should also prefix the 's/?' or 's/!" with -e if you do this, then add -e 's/$/@/'. This applies generally to various sed solutions in the answers. – Andy Weinstein Jun 05 '16 at 15:59
  • Perfect answer to add repo – VVB Aug 19 '16 at 11:09
  • 1
    instead of `sed 's/? //'` you could use `awk {'print $2'}` – Jijo Feb 28 '18 at 19:26
  • @AndyWeinstein, or you can just use `awk '{print $2"@"}'` isntead of all this sed boilerplate, in case your paths contain @ in them. – Alex Che Apr 06 '20 at 15:07
  • Slightly modified version that both strips the ? & whitespace better, but also allows batching in case of large numbers of files, only calls `svn add` if there are items in the list & uses new lines as a delimeter as they are more reliable than tabs. `svn st | grep ^? | cut -d? -f2 | sed 's/^ *//' | xargs -n 500 -d "\n" -r svn add` – Daniel Iser Mar 02 '22 at 09:06
  • @AlexChe @Jijo your `awk` invocations break when there are spaces in file names. – JDS Apr 03 '23 at 14:06
34

you can just do an svn add path/to/dir/* you'll get warning about anything already in version control but it will add everything that isn't.

Earlz
  • 62,085
  • 98
  • 303
  • 499
prodigitalson
  • 60,050
  • 10
  • 100
  • 114
  • 34
    You really don't want to do this if you have ignored files because it will re-add them. – phazei Sep 07 '10 at 20:44
  • 8
    http://svnbook.red-bean.com/en/1.6/svn.ref.svn.c.add.html --> "Normally, the command svn add * will skip over any directories that are already under version control. Sometimes, however, you may want to add every unversioned object in your working copy, including those hiding deeper. Passing the --force option makes svn add recurse into versioned directories" – ilmatte Jun 27 '14 at 13:13
11
svn status | grep "^\?" | awk '{print $2}' | xargs svn add

Taken from somewhere on the web but I've been using it for a while and it works well.

markb
  • 3,451
  • 5
  • 24
  • 25
  • @phazei 's answer is more complete (http://stackoverflow.com/questions/2120844/how-do-i-add-all-new-files-to-svn/3662677#3662677) – Alexander Bird Nov 06 '10 at 15:48
  • A like this version better, over @phazei's, because this version uses `awk` and exempts you from counting spaces. – Alex Che Apr 06 '20 at 15:03
6

If svn add whatever/directory/* doesn't work, you can do it the tough way:

svn st | grep ^\? | cut -c 2- | xargs svn add
Carl Norum
  • 219,201
  • 40
  • 422
  • 469
4

The solution

svn status | grep ^? | sed 's/?    //' | xargs svn add

does not work with whitespaces. Instead one can use

svn status | grep ^? | sed 's/^?       //' | xargs -I fn svn add "fn"

(seems like the number of leading blanks is different on my system -- just adjust it).

Stefan Witzel
  • 335
  • 2
  • 10
  • I use this method. Seems to work the best and will legitimately add the appropriate files, as opposed to indiscriminately forcing all files to add. thanks! – Grapho Mar 17 '15 at 14:34
4

You should be able to run:

svn add *

It may complain about the files that are already under version control, but it will also add the new ones.

You may want to think about whether or not you really want to add these generated files to version control, though. They could be considered derived artifacts, sort of like the compiled code, and thus shouldn't be added. Of course, this is up to you, but its something to think about.

pkaeding
  • 36,513
  • 30
  • 103
  • 141
3
svn add *

should do the job. Just make sure to:

svn commit

afterwards :)

ryanprayogo
  • 11,587
  • 11
  • 51
  • 66
2

On Alpine Linux OS I used this, based on others answers:

svn st | grep ^? | sed 's/? *//' | xargs -I fn svn add "fn"
arvati
  • 149
  • 1
  • 5
1

I like these commands as they use svn status to find the new or missing files, which respects files that are ignored.

svn add $( svn status | sed -e '/^?/!d' -e 's/^?//' )

svn rm $( svn status | sed -e '/^!/!d' -e 's/^!//' )
Tristan
  • 11
  • 1
1

In some shells like fish you can use the ** globbing to do that:

svn add **
Michael
  • 8,920
  • 3
  • 38
  • 56
1

This add all unversioned files even if it contains spaces

svn status | awk '{$1=""; print $0}' | xargs -i svn add "{}"

1
svn status | grep "^\?" | awk '{ printf("\""); for (f=2; f <= NF; f++) { printf("%s", $f); if (f<NF) printf(" "); } printf("\"\n");}' | xargs svn add

This was based on markb's answer... and a little hunting on the internet. It looks ugly, but it seems to work for me on OS X (including files with spaces).

Ron
  • 3,055
  • 1
  • 20
  • 21
0

Among bash one-liner I think this is the prettiest:

svn status | tee >(awk '/^?/{print $2}' | xargs -r svn add >&2) | awk '/^!/{print $2}' | xargs -r svn delete

It will add all new files and delete all missing files. Use with caution, possibly set an alias for quick access.

NOTE for Macs: in xargs -r is a GNU extension: it might not be supported. In that case just remove it and ignore warnings when there are no files to add or to delete

Andrea Ratto
  • 815
  • 1
  • 11
  • 23
0

I am a newbie to svn version control. However, for the case when people want to add files without ignoring the already set svn:ignore properties, I solved the issue as below

  1. svn add --depth empty path/to/directory
  2. Execute "svn propset svn:ignore -F ignoreList.txt --recursive" from the location where the ignoreList.txt resides. In my case this file was residing two directories above the "path/to/directory", which I wanted to add. Note that ignoreList.txt contains the file extensions I want svn to ignore, e.g. *.aux etc.
  3. svn add --force path/to/directory/.

The above steps worked.