[svn] How do I 'svn add' all unversioned files to SVN?

I'm looking for a good way to automatically 'svn add' all unversioned files in a working copy to my SVN repository.

I have a live server that can create a few files that should be under source control. I would like to have a short script that I can run to automatically add these, instead of going through and adding them one at a time.

My server is running Windows Server 2003 so a Unix solution won't work.

This question is related to svn command-line windows-server-2003

The answer is


svn add --force .

This will add any unversioned file in the current directory and all versioned child directories.


This is the lazy and dangerous way to synchronize a directory with SVN including new files:

svn rm --keep-local dir
svn add dir

Although this can work in a pinch it can have serious consequences as well. For example, SVN will often lose track of a file's history.

The ideal way to syncronize a directory would be to be able to diff then use svn patch, however this deviates from formats in the diff command and the svn diff command will compare working directory differences rather than differences on the file system level.


I always use:

Copy&paste

svn st | grep "^\?" | awk "{print \$2}" | xargs svn add $1

This is a different question to mine but there is an answer there that belongs on this question:

svn status | grep '?' | sed 's/^.* /svn add /' | bash

This method should handle filenames which have any number/combination of spaces in them...

svn status /home/websites/website1 | grep -Z "^?" | sed s/^?// | sed s/[[:space:]]*// | xargs -i svn add \"{}\"

Here is an explanation of what that command does:

  • List all changed files.
  • Limit this list to lines with '?' at the beginning - i.e. new files.
  • Remove the '?' character at the beginning of the line.
  • Remove the spaces at the beginning of the line.
  • Pipe the filenames into xargs to run the svn add multiple times.

Use the -i argument to xargs to handle being able to import files names with spaces into 'svn add' - basically, -i sets {} to be used as a placeholder so we can put the " characters around the filename used by 'svn add'.

An advantage of this method is that this should handle filenames with spaces in them.


I think I've done something similar with:

svn add . --recursive

but not sure if my memory is correct ;-p


If you use Linux or use Cygwin or MinGW in windows you can use bash-like solutions like the following. Contrasting with other similar ones presented here, this one takes into account file name spaces:

svn status| grep ^? | while read line ; do  svn add "`echo $line|cut --complement -c 1,2`" ;done

What works is this:

c:\work\repo1>svn add . --force

Adds the contents of subdirectories.

Does not add ignored files.

Lists what files were added.

The dot in the command indicates the current directory, this can replaced by a specific directory name or path if you want to add a different directory than the current one.


After spending some time trying to figure out how to recursively add only some of the files, i thought it would be valid to share what did work for me:

FOR /F %F IN ('dir /s /b /a:d') DO svn add --depth=empty "%F"
FOR /F %F IN ('dir /s /b /a *.cs *.csproj *.rpt *.xsd *.resx *.ico *.sql') DO svn add "%F"

Here goes some explanation.

The first command adds all the directories. The second command adds only the files accordingly to the specifed patterns.

Let me give more details:

  • FOR: you know, the loop control.
  • /F: means take the files and directories (not sure exactly).
  • %F: its a variable; it will assume the value of each of the listed files at a time; it could have another one-character-name.
  • IN: no need to explain, right?
  • ('dir /s /b /a:d'): the DOS command that will list the directories; in my case /s is recursive, /b is to take only the full path, /a:d means only the directory; change it as you wish keeping the parenthesis and apostrophes.
  • DO: means that what comes next in the command is what will be executed for each directory
  • svn add --depth=empty: it is the desired SVN commando to be run; the depth definition means to add only the directory and not the files inside them.
  • "%F": that's how you use the variable defined earlier.

In the second command, the only differences are the dir command and the svn command, i think it is clear enough.


You can input the following command on Linux:

find ./ -name "*." | xargs svn add

You can use command

svn add * force--

or

svn add <directory/file name>

If your files/directories are not adding recursively. Then check this.

Recursive adding is default property. You can see in SVN book.

Issue can be in your ignore list or global properties.

I got solution google issue tracker

Check global properties for ignoring star(*)

  • Right click in your repo in window. Select TortoiseSVN > Properties.
  • See if you don't have a property svn:global-ignores with a value of *
  • If you have property with star(*) then it will ignore recursive adding. So remove this property.

Check global ignore pattern for ignoring star(*)

  • Right click in your repo in window. Select TortoiseSVN > Settings > General.
  • See in Global Ignore Pattern, if you don't have set star(*) there.
  • If you found star(*), remove this property.

This guy also explained why this property added in my project.

The most like way that it got there is that someone right-clicked a file without any extension and selected TortoiseSVN -> SVN Ignore -> * (recursively), and then committed this.

You can check the log to see who committed that property change, find out what they were actually trying to do, and ask them to be more careful in future. :)


Since this post is tagged Windows, I thought I would work out a solution for Windows. I wanted to automate the process, and I made a bat file. I resisted making a console.exe in C#.

I wanted to add any files or folders which are not added in my repository when I begin the commit process.

The problem with many of the answers is they will list unversioned files which should be ignored as per my ignore list in TortoiseSVN.

Here is my hook setting and batch file which does that

Tortoise Hook Script:

"start_commit_hook".
(where I checkout) working copy path = C:\Projects
command line: C:\windows\system32\cmd.exe /c C:\Tools\SVN\svnadd.bat
(X) Wait for the script to finish
(X) (Optional) Hide script while running
(X) Always execute the script

svnadd.bat

@echo off

rem Iterates each line result from the command which lists files/folders
rem     not added to source control while respecting the ignore list.
FOR /F "delims==" %%G IN ('svn status ^| findstr "^?"') DO call :DoSVNAdd "%%G"
goto end

:DoSVNAdd
set addPath=%1
rem Remove line prefix formatting from svn status command output as well as
rem    quotes from the G call (as required for long folder names). Then
rem    place quotes back around the path for the SVN add call.
set addPath="%addPath:~9,-1%"
svn add %addPath%

:end

This is as documented on svn book and the simplest and works perfect for me

svn add * --force

http://svnbook.red-bean.com/en/1.6/svn.ref.svn.c.add.html


This worked for me:

svn add `svn status . | grep "^?" | awk '{print $2}'`

(Source)

As you already solved your problem for Windows, this is a UNIX solution (following Sam). I added here as I think it is still useful for those who reach this question asking for the same thing (as the title does not include the keyword "WINDOWS").

Note (Feb, 2015): As commented by "bdrx", the above command could be further simplified in this way:

 svn add `svn status . | awk '/^[?]/{print $2}'`

for /f "usebackq tokens=2*" %%i in (`svn status ^| findstr /r "^\?"`) do svn add "%%i %%j"

Within this implementation, you will get in trouble in the case your folders/filenames have more than one space like below:

"C:\PROJECTS\BACKUP_MGs_via_SVN\TEST-MG-10\data\destinations\Sega Mega      2"
"C:\PROJECTS\BACKUP_MGs_via_SVN\TEST-MG-10\data\destinations\One space"
"C:\PROJECTS\BACKUP_MGs_via_SVN\TEST-MG-10\data\destinations\Double  space"
"C:\PROJECTS\BACKUP_MGs_via_SVN\TEST-MG-10\data\destinations\Single"

such cases are covered by simple:

for /f "usebackq tokens=1*" %%i in (`svn status ^| findstr /r "^\?"`) do svn add "%%j"

TortoiseSVN has this capability built in, if you're willing to use a non-command-line solution. Just right click on the top level folder and select Add...


Use:

svn st | grep ? | cut -d? -f2 | xargs svn add

Since he specified Windows, where awk & sed aren't standard:

for /f "tokens=1*" %e in ('svn status^|findstr "^\?"') do svn add "%f"

or in a batch file:

for /f "tokens=1*" %%e in ('svn status^|findstr "^\?"') do svn add "%%f"

Examples related to svn

Error "can't use subversion command line client : svn" when opening android project checked out from svn How to view changes made to files on a certain revision in Subversion Intellij idea subversion checkout error: `Cannot run program "svn"` How change default SVN username and password to commit changes? How to rename a file using svn? Connect Android Studio with SVN svn: E155004: ..(path of resource).. is already locked SVN Commit failed, access forbidden How to add an existing folder with files to SVN? Update OpenSSL on OS X with Homebrew

Examples related to command-line

Git is not working after macOS Update (xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools) Flutter command not found Angular - ng: command not found how to run python files in windows command prompt? How to run .NET Core console app from the command line Copy Paste in Bash on Ubuntu on Windows How to find which version of TensorFlow is installed in my system? How to install JQ on Mac by command-line? Python not working in the command line of git bash Run function in script from command line (Node JS)

Examples related to windows-server-2003

Multiple -and -or in PowerShell Where-Object statement Batch file to restart a service. Windows Error message: (provider: Shared Memory Provider, error: 0 - No process is on the other end of the pipe.) Domain Account keeping locking out with correct password every few minutes What possibilities can cause "Service Unavailable 503" error? ORA-12505: TNS:listener does not currently know of SID given in connect descriptor (DBD ERROR: OCIServerAttach) how to get list of port which are in use on the server How do I 'svn add' all unversioned files to SVN? Windows command to get service status? How do I measure execution time of a command on the Windows command line?