The basics of SVN

Subversion (SVN) is a version control system that helps manage changes to files and directories over time. It allows multiple people to collaborate on a project and keeps a history of every change made to files and directories.

1. Introduction to SVN

Subversion (SVN) is a version control system. Like CVS, SVN is cross-platform and supports most common operating systems. As an open-source version control system, Subversion manages data that changes over time. This data is stored in a central repository, which functions like a regular file server but remembers every change made to files. This allows you to revert files to previous versions or browse the history of changes. Subversion is a general-purpose system and can be used to manage any type of files, including program source code.

2. Installing SVN

Installation Guide: Installing SVN server on Linux.

3. Using SVN

3.1. Checking out files to a local directory

svn checkout svn_path local_path
# For example:
svn checkout svn://192.168.1.131/45dian/brand
# Recommended to add local directory:
svn checkout svn://192.168.1.131/45dian/brand ./brand/
# Shortened version
svn co

3.2. Adding new files to the repository

svn add file
# For example (adding test.php):
svn add test.php 
# Adding all php files in the current directory
svn add *.php
# Adding user directory (recursively adding all contents within)
svn add user

After adding files, they need to be committed to the repository.

3.3. Committing changes to the repository

svn commit -m 'Comment' [-N] [--no-unlock] PATH
# Shortened version
svn ci
# Committing a file and directory
svn ci -m 'Added new file' test.php
svn ci -m 'Added new directory (recursively)' user

3.4. Locking/Unlocking

svn lock -m 'Lock comment' [--force] PATH
# For example:
svn lock -m "Locking file" test.php
# Unlocking
svn unlock PATH 

3.5. Updating to the latest version

Before modifying files, always update the repository, then modify the files, and finally commit them. If there is a conflict during commit, update first, resolve conflicts, and then commit again.

svn update -r m PATH
# Updating to the latest version:
svn update
# Reverting a file to a historical version 200
svn update -r 200 test.php
# Updating test.php to the latest version
svn update test.php
# Shortened version
svn up

3.6. Checking the status of files or directories

svn status PATH
# Displays the status of files and subdirectories, shows nothing if normal
# ? Not under SVN control
# M Modified
# C Conflict
# A Scheduled to be added to repository
# K Locked
svn status -v PATH
# For example:
svn status
svn status -v
# Shortened version
svn st

3.7. Deleting files

svn delete PATH -m 'Comment'
# For example:
svn delete svn://192.168.1.133/45dian/brand/test.php -m 'Deleting file from SVN'
# Or (recommended)
svn delete test.php
svn ci -m 'Committed deleted file'
# Shortened version
svn (del, remove, rm)

3.8. Viewing logs

svn log PATH
# For example:
# Displays the modification history and version changes of a file
svn log
svn log test.php

3.9. Viewing detailed information about a file

svn info PATH
# For example:
# Displays information about the current directory
svn info
# Displays information about test.php
svn info test.php

3.10. Comparing file or directory differences

svn diff PATH
# Comparing the modified file with the latest version in the repository
svn diff test.php

# Comparing different versions
svn diff -r m:n PATH
# Comparing differences between version m and version n
svn diff -r 200:201 test.php

3.11. Merging differences between two versions into the current file

# Merging changes from version m to version n into the current file
svn merge -r m:n PATH
# For example:
svn merge -r 200:201 test.php
# Usually results in conflicts that need to be resolved

3.12. SVN Help

svn help
svn help ci

3.13. Adding directories to the repository

# Adding a directory to the SVN repository
svn mkdir PATH
# Equivalent to:
mkdir work
svn add work -m 'Added directory'

3.14. Changing the repository URL

svn switch (sw): Updates the working copy to a different URL.
Usage:
    1. switch URL [PATH]
    2. switch --relocate FROM TO [PATH...]

    1. Updates your working copy to map to a new URL, similar to “svn update”, merging changes from the server with local changes. This method maps the working copy to a different branch or tag within the same repository.
    2. Rewrites the working copy’s URL metadata to reflect a simple URL change. Use this command to update the working copy’s mapping to the repository if the repository root URL changes (e.g., scheme or hostname changes) but the working copy still points to the same directory in the repository.

3.15. Resolving Conflicts

svn resolved: Removes the “conflict” status from a working copy directory or file.
Usage: resolved PATH…
Note: This command does not resolve conflicts or remove conflict markers; it simply removes the conflict-related files so that PATH can be committed again.