Subversion (SVN) command summary cheat sheet, use, best practices, tips and scripts.
This tutorial covers version control with Subversion using
the command line interface, GUI clients and scripts which interface Subversion with GUI diff tools.
Subversion is a software source code "Change Management" (CM)
system
for collaborative development. It maintains a history of file and
directory versions.
The files and directories are checked out of the repository and into
your local project work area. This called your "working directory".
Changes are made to files in your "working directory". After changes
are made to the create the next working version, the files are checked
into the Subversion CM repository.
Subversion Commands:
Subversion command summary cheat sheet:
Command
Description
svn --help
List Subversion commands
svn help command Also: ? or h
Help on given "command"
svn addfilename
svn add directory
Add a file or directory to Subversion CM control.
Must also perform: svn ci filename (or svn commit)
to upload the file or directory. File will not be available in the
repository until a "commit" is performed. If adding a directory, the
directory and all of its contents recursively are added. i.e.: svn ci directory
svn commit directory
svn commit .
svn blamefilename svn blame -r RevisionNumberfilename
Also: praise, annotate, ann
Show file contents with revisions annotated with author information.
svn catfilename
List contents of file under Subversion control.
svn checkout http://node-name/repos/svn/trunk/parentPath/path
This creates: path/file1
path/file2
...
svn checkout http://node-name/repos/svn/trunk/parentPath .
This creates: path/file1
path/file2
...
Note the difference a "." makes.
svn checkout file:///repos/svn/trunk/path/ svn co -r 497 http://node-name/repos/svn/trunk/pathfile-name
Also: svn co https://..., svn://..., and svn+ssh://
MS/Windows: svn co file:///c:/repository/project/trunk
Checkout every file from the path and subdirectories specified below. Creates "working" copy of files and directories.
Checkout a repository.
Use option "-r" to specify a specific revision other than the latest.
The URL "svn://" communicates with an SVN server (port 3690)
The URL "http://" comunicates with the Apache server and module mod_dav_svn (port 80) [more common server]
svn cleanup
Cleanup subversion files resulting from escaped processes and crashed.
Check-in (commit) local "working" file,
files or directory and contents (recursively) into Subversion
repository. Atomic, i.e. all committed or none, no incomplete check-in.
svn copysourcedestination_clone
Also: svn cp ...
Copy file or directory tree. One can copy
from one local working copy to another or to repository server URL's.
The sources and destinations can be working copies or URLs.
Tag a release. Takes a snapshot of your local working copy and assigns a name. This can be performed at any directory branch.
svn deletefilename svn delete directory
Also: del, remove or rm svn rm http://host/repos/project/trunk/file-or-directory
Delete file from repository. The UNIX command rm file-name. Must perform a "commit" to update the repository and local working directory with the changes. i.e.: svn commit .
svn difffilename
svn di filename
Show file diffs between SVN repository and your file changes using GNU file diff format. Use GUI diff tools as shown below.
svn diff -r rev1:rev2filename
Show file diffs between specified versions.
Example: svn diff -r 456:459 subfn.cpp
Using GUI diff tool: svn diff -r 457:459 --diff-cmd kdiff3 file-name
Add directory (and files in it recursively) to path in repository specified.
svn infofilename
Display information about file or directory. (Date modified, author, revision, path in repository.)
Can not specify a URL.
svn listdirectory
svn list file-name
List file or directory of files in repository. Used to browse repository before checkout. If current directory is given (svn list ./), then Subversion will list the repository URL of the current directory.
svn list -r RevisionNumberdirectory
List directory of files in repository in specified revision.
svn lockfilename -m "comment as to why its locked or by whom"
(Comment is not required but is often useful)
Lock file to grant exclusive access to one and forbid all others. A commit will unlock the file (unless the "--no-unlock" option is used). A lock can be removed with the commands: svn unlock filename, svnlook and the svnadmin comands (i.e. List: svnadmin lslocks and remove: svnadmin rmlocks filename).
Show the Subversion log messages for a set of revision(s) and/or file(s) and/or all directory contents in repository.
List verbose. Includes list of all files in change
Shows the file changes associated with revision number.
Merge directory changes into your current
working directory or merge a file in Subversion into the file in your
working directory. If target is not specified, the identical basename
or current directory is assumed. Used to incorporate changes checked in
which are not accounted for in your file or to merge branches.
Example using GUI merge tool: svn diff -r 459:454 --diff-cmd kdiff3 --extensions '-m' file-name
Next, tell subversion that the conflicts have been resolved: svn resolve file-name
Finally, check-in file: svn ci file-name
or abort changes: svn revert file-name
svn merge --dry-run -r 414:413 http://url/path
Test merge. No changes are made to your local working copy but shows Subversion feedback as if merge was performed.
Rename or move a file or directory. Moves/renames file/directory in repository and in local work area.
Must perform svn ci file-new-name after the move for changes to to take place in repository.
svn revertfilename
Undo changes in local work files. Throw away local changes.
svn resolvedfilename
Run this command after resolving merge conflicts. Next "commit" your changes.
svn status
svn status -u
svn status -u .
svn status -uq .
Show status of file changes in current directory and recursively in directories below.
Show out of date file info: svn status --show-updates
(equivalent: svn status -u)
-u: Determines status by comparing your local repository with the server
repository. Without this option, the status shown will only be the changes
you have made in your local repository.
-q: Quiet. Do not print "?: File/directory not under version control" or "!: File/directory missing" extraneous information.
First collumn:
U: File to be updated
A: File to be added
D: File to be deleted
R: File to be replaced
G: File to be merged
C: Conflicting changes
X: Resource is external to repository (svn:externals)
?: File/directory not under version control
!: File/directory missing
Second collumn: Modification of properties
' ' no modifications
'C' Conflicted
'M' Modified
'*' Local file different than repository. File will
be overwritten with the updated version from the server, if you perform
an update.
Switch your local working copy to mirror a
new repository branch instead of main trunk or previous branch. Also
allows you to point your repository to a new path on the server if the
server path changes since you performed a check-out.
Migrate all updates from Subversion
repository to your local copy (recusively for all files in the current
directory and all below it). If there have been updates to the svn
repository since you downloaded the files, subversion will give you the
opportunity to merge. Status of files will use the coding as stated
above for "status". Files marked with a "C" (conflict) should be merged
of reverted. If merged then one can perform a "resolve" and then a
"check-in".
If a file name is specified, only that file is updated.
Can also syncronize to a specified revision given by -r.
Use --ignore-externals to avoid the slow processing of externals to a potentially slow distant internet server.
Where RevisionNumber is:
HEAD: The latest revision in the repository.
BASE: The "pristine" revision of an item in a working copy. Matches checked out version before any modifications.
COMMITTED: The last revision in which an item changed before (or at) BASE.
PREV: The revision just before the last revision in which an item changed. (Technically, COMMITTED - 1.)
Example Session:
(Assumes that the repository has already been created. For Subversion
repository creation and Subversion server configuration, see the (YoLinux Subversion and Trac tutorial)
Verify and test: make
We are ready to check-in the files into the Subversion repository.
Check repository and report on new revisions and changes others have checked in: svn status -u .
After many long hours or days of editing and work, get updates others have made: svn update
U file.h C file1.cpp G file2.cpp ? a.out
You will see:
U: File was updated with a newer version checked-in since your checkout.
G: Automatically merged with no conflicts.
C: Not merged due to conflicts. You made changes to the
same section of code as the update made by someone else since your
checkout.
For each "conflicted" file there will be three new local files generated by "update":
file1.cpp.mine (File - post editing)
file1.cpp.rold (BASE - pre editing)
file1.cpp.rnew (HEAD - Updated file from repository)
The file file1.cpp still exists but with svn conflict marker strings added in the file.
At this point, a check-in will fail until the merge is resolved.
Merge options:
Edit the file file1.cpp
Text markers are placed in the file to show the conflicts between the "HEAD" and "mine" versions.
OR
tkdiff -conflict file1.cpp
OR
Use a GUI merge tool: kdiff3 file1.cpp.mine file1.cpp.rnew -o file1.cpp
OR
Throw out your changes/abort: svn revert file1.cpp
No resolve or check-in necessary if file is reverted.
Verify and test, again: make
Notify Subversion that conflicts have been resolved: svn resolved file1.cpp
Note: This also removes the temporary files ".mine" and ".r###".
Check-in to Subversion repository: svn ci -m "Add comments here" file1.cpp
Subversion Properties:
Files under revision control can include Subversion keywords which
properties can be set with the "propset" command.
Keywords are substituted with the Subversion properties and
will not appear in the file until a commit is performed.
Other properties are used to modify the behavior of Subversion.
The following properties can be set on entities stored in Subversion:
Property
Description
svn:ignore
A newline separated list of file patterns to ignore. List of files/directories to be ignored by svn status
svn:keywords
Valid RCS style keywords are:
HeadURL - The URL for the head version of the object.
Also: $URL$
LastChangedBy - The last person to modify the file.
Also: $Author$
LastChangedDate - The date/time the object was last modified.
Will appear as: $LastChangedDate: 2005-07-22 22:02:37 -0700 (Fri, 22 Jul 2005) $
Also: $Date$
LastChangedRevision - Describes the last known revision.
Will appear as: $LastChangedRevision: XXX $ where "XXX" is the revision number.
Also: $Rev$, Revision
$Id$ - A compressed summary of the previous 4 keywords.
See file /etc/mime.types for a list of mime types.
Examples:
svn propset svn:mime-type text/plain file.cpp
svn propset svn:mime-type text/html file.html
Web pages rendered from subversion will display as HTML source rather
than as a web page unless this mime type is applied.
svn:needs-lock
Prevents conflicts for files which can not be contextually merged. i.e. photos, binaries, object libraries.
svn:externals
List of files or directories pointed to. Locate repository where directory specified should be retrieved. The directive propset is not required:
The property applies to the directory. Subversion can
not list or web browse svn:externals. Check-out ("co"), "export" and
"log" can be performed.
Must set environment variable "EDITOR", "SVN_EDITOR", "VISUAL" or set the Subversion configuration file (~/.subversion/config) attribute editor-cmd. i.e.: export EDITOR=vi
[Potential Pitfall]: Apply svn externals to directories and not files. (version 1.4)
[Potential Pitfall]: If generating a tag (branch), one should assign the revision number to the external link to truly snapshot the repository for that tag.
This revision number should be specified in the tagged branch version of the svn external.
[Potential Pitfall]: The revision numbers shown by svn info will reflect the version numbers of the repository in which the files are stored.
If the files are imported via an svn external directory, the revision numbers will reflect that of the external repository which the external points to and thus from where the files were imported.
Three types of file differences are covered in this section:
Show file differences made since checkout was made. This
shows the changes you have made. It is usefull to perform this before
an update.
Show file differences between two files versions stored in Subversion.
Show differences / conflicts, choose and merge. Use our
bash script svndiffwrapper which integrates into Subversion's file
check-in process.
1) File differences since checkout:
The following scripts will allow you to view the changes you have made since
checkout. Use the script before running "svn update" as an update will alter the file with diff chevron (>>>>) markers. After performing a "svn update", use tkdiff, gtkdiff or kdiff3 as described below instead of the scripts:
Use the following bash shell script to use the graphical diff tool "mgdiff" with Subversion.
svndiff:
#!/bin/bash
# svndiff 1.0
# usage: svndiff file
if [[ ! -d .svn ]]
then
echo ERROR: You are not working in an SVN directory.
exit 1
fi
rev="--revision HEAD"
if [[ ! -n $1 ]]
then
echo "Usage: svndiff [option] file"
echo "Options:"
echo " -h Diff with latest in repository (HEAD) - Default"
echo " -b Diff with what you had checked out (BASE)"
echo " -c Diff with COMMITTED, the version before BASE"
echo " -p Diff with PREV, the version before COMMITTED"
echo " -r revnum Diff with specified revision (specify integer)"
exit 1
fi
while getopts ":r:hbcp" Option
do
case $Option in
h) rev="--revision HEAD";;
b) rev="--revision BASE";;
c) rev="--revision COMMITTED";;
p) rev="--revision PREV";;
r) rev="--revision $OPTARG";;
*) echo "Incorrect option specified. Use -h or -b or -r #";;
esac
done
shift $(($OPTIND -1))
# Define graphical diff tool
#
# The geometry option used by Motif, Tcl and X based programs
geometry="-geometry 1280x800+0+0"
# The following is for Motif diff
# -w: ignore white space
dif="mgdiff -args -w"
file=$1
prev=${file}_PREV
# Trap bash command signals
# SIGINT 2
# SIGQUIT 3
# SIGTERM 15
trap "rm -f $prev" 2 3 15
svn cat $rev $file > $prev 2>/dev/null
$dif $geometry $prev $file
sleep 1
rm -f $prev
Compares your current local copy with the latest in the repository (Default "-h").
OR
Use the following bash shell script to use the graphical diff tool "gvimdiff:" with Subversion:
svndiff:
#!/bin/bash
# usage: svndiff file
if [[ ! -d .svn ]]
then
echo ERROR: You are not working in an SVN directory.
exit 1
fi
# Define graphical diff tool
#
dif="gvimdiff \"+colo morning\" -R"
file=$1
prev=PREV_${file}
# Trap bash command signals
# SIGINT 2
# SIGQUIT 3
# SIGTERM 15
trap "rm -f $prev" 2 3 15
svn cat $file > $prev 2>/dev/null
$dif $prev $file
# Sleep for non-blocking apps like gvimdiff.
# Allow gvimdiff to read file before it is deleted.
sleep 2
rm -f $prev
Compare your current local copy with the original copy you checked out.
2) File differences between two revisions:
This configuration supports the use of GUI diff tools with Subversion by using
the command: svn diff -r 457:459 --diff-cmd
Some diff tools are supported with native svn. i.e.: svn diff -r 457:459 --diff-cmd kdiff3 file-name while others require a wrapper script to
arrange the arguments correctly.
Subversion configurations and defaults are specified in the file: $HOME/.subversion/config
This configuration configures Subversion to execute the script
/opt/bin/diffScript to launch your own diff toolwith the command:
svn diff -r Old:New URL.
Use our bash script svndiffwrapper to perform this integrated task. By default, uses kdiff3 for diff tool. Can also specify your own choice of diff tool.
Place the script in /opt/bin/ for global use or $HOME/bin/
for private user access
and set permissions so that script execution is allowed: chmod ugo+x /opt/bin/svndiffwrapper
List of graphical diff and merge tools:
tkdiff: [download] Comes with tkcvs Subversion GUI front-end.
Examples of Subversion diffs with tkdiff: (See: tkdiff --help)
tkdiff old-URL@revA new-URL@revB
tkdiff -r457 -r459 file-name
Note: Use command "svn log file-name" to view valid revision numbers for the file. Referencing invalid revision numbers as input to tkdiff will not work.
gtkdiff: Has diff3 and merge features. Written with GTK+. After gtkdiff-0.8.0, GNOME desktop required.
diffUse: Diff/merge GUI tool. Good line matching features. Supports Unicode.
kdiff3:
Graphical directory and file diff, merge and edit. KDE3/Qt based.
Supports drag and drop. Comes with S.u.S.E. distro. (Cross platform)
MS/Windows download available. A very good directory and file diff and
merge tool.
Difference: kdiff3 file1 file2
Difference: kdiff3 file1 file2 file3
Difference of two files: kdiff3 directory1/file directory2
TkSVN / TkCVS:
Tcl/Tk based GUI. A very good Unix/Linux and MS/Windows GUI front-end
to Subversion. Simple to install (requires tk). Supports GUI
diff/merge, branching, tagging, editing, check-in/check-out, ...
Installation to /usr/local/bin and lib (Add to your path.): (requires RPM: tk version 8.4+)
tar xzf tkcvs_8_0_3.tar.gz
cd tkcvs_8_0_3
./doinstall.tcl -nox /opt
Configuration: (See: ~/.tkcvs)
Set default editor and diff tool:
...
set cvscfg(editor) "xterm -e vim"
set cvscfg(tkdiff) "tkdiff"
Also see default config file: /opt/lib/tkcvs/tkcvs_def.tcl
Sets default editors for various file types. Set editor to "gedit" for rookies.
If you want to run TkSVN / TkCVS on MS/Windows, download
Tk for MS/Windows. It can also be run from the Cygwin environment using Tk provided in the Cygwin shell.
One should be aware of a possible Subversion client security hole.
The Subversion client authentication will cache your login and password in your home directory in non-encrypted clear text.
It will have file system security so others can not read the file however,
it still is visible by root or by a root user of the file server if one is used for home directories.
Here are three solutions to reduce this potential security vulnerability:
Don't allow Subversion to cache the password:
svn commit -F file.txt --no-auth-cache
This will request a username and password but will not store it.
Set the Subversion configuration file on server to not cache:
[auth]
store-auth-creds = no
Delete cached files when you logout. Reduces risk but does not eliminate it.
This uses the bash shell logout script to perform a clean-up of the authentication files.
File: ~/.bash_logout
rm ~/.subversion/auth/svn.simple/*
Subversion utility commands and scripts:
Command
Description
svnversion local-path
This svn admin command will produce a compact
version number for a working copy. Lists range of versions, adds "S" if
switched, "M" modified.
ALWAYS compile and test before checking in source code. Subversion revisions should correspond to revisions which compile.
Don't copy, rename and move directories and files with
system shell commands. Use Subversion commands to "rm", "mv", and "add"
directories and files and then commit changes when done. Work within
Subversion.
Commit changes as a single logical changeset for one
purpose. Thus all code changes for a single bug fix or enhancement
should be checked-in together.
This allows one to better follow the history log of changes.
Check-in code at the directory level and all changed files,
recursively in the directory and subdirectories will be checked in
together. svn ci -m "Check-in comment goes here" ./
Check-in files together by specifying them explicitly:
svn ci -m "Check-in comment goes here" file.cpp file2.cpp ...
Tie Bug tracking and Subversion CM together:
Use comments when checking-in files into Subversion. Add bug
tracking numbers to the comments so Subversion will reference Trac
bugs.
If using Trac, add trac comments so that links are
generated to the Subversion repository by placing the Subversion
revision number in square braces (i.e. [1140]) in a Trac comment. In
this way, Trac will have a direct URL link to Subversion.
If using Trac integrated with Subversion, refer to the Trac ticket
in the Subversion check-in comment using a "#" in front of the Trac ticket number (eg. #65) This generates a hyperlink when the Subversion logs are viewed in Trac.
Taking code from a Subversion repository for upload to another: Use "svn export" and NOT "svn co" if you want files for upload into another CM repository.
A checkout (svn co) will create Subversion management directories (.svn/)
in the local working copy. You will not want to upload these
directories into a CM system as they are only for the user's local
working directory only. An "export" will not create these directories.
The "tags" branches are NOT to be used as working branches but are snapshots of an existing branch. The "tags" are for historical reference such as a release, well tested version or progress milestone.
Documentation and related artifacts should not be under the source tree but parallel to it.
This isolates the source tree so that email notification triggers sent to developers upon source changes will only go out on source changes or regression build and test systems like Cabie will only rebuild and test on source changes rather than on unrelated documentation changes.
Version Control with Subversion
by C. Michael Pilato
ISBN #0596004486, O'Reilly Press
Practical Subversion (Expert's Voice in Open Source)
Garrett Rooney
ISBN #1590592905, Apress
Subversion Version Control: Using the Subversion Version Control System in Development Projects
William Nagel
ISBN #0131855182, Prentice Hall PTR
Dr. Dobb's Journal
Free subscription to the premier resource for
professional programmers and software developers. Multi-language and
multi-platform with program listings, coding tips, design issue
discussions and algorithms. Subscribe here!