How to Force Lock and Unlock Files in SVN with Command Examples

by Luke P. Issac on July 29, 2014

Sometimes when you try to commit a file to SVN repository, you might get a conflict message.

This means that your working copy is not the latest one when compared to the copy that was already present in the repository.

Someone has updated the file after you took the latest update from the repository.

If it is a text file (scripting file, programming language file, readme file, etc.), you can take an update and probably still can commit without any conflict as your changes will be merged automatically to the latest file that is present in the repository. If the updates are done on the same line, then you can probably manually resolve the conflict by comparing both the files.

SVN merge by itself is a detailed topic, which we explained earlier by providing several examples on how to merge svn branch and trunk.

Now, what if the same situation occurs when you are trying to commit a binary file (for example, a image file). This file is being frequently modified by different people in your team and here you can’t manually resolve the conflict like you did with the text files.

This is where SVN locks come into play. It flags different users collaborating with each other by stating that the current file is in locked state. i.e. someone is modifying it, so please wait for that person to commit it first.

If you have locked a file, it cannot be modified by any other users straight away. So as soon as you are taking an update, if you are getting a svn lock on it, you can make others aware that you are modifying so please don’t commit any changes from your side. Even svn will not allow them to commit their changes in normal way.

This article explains everything that you need to know about how to manage locks on files in your SVN repository.

1. View SVN Lock Status

SVN status command will display all the files in the current directory. The output of this command will have six columns of one letter code as prefix for each file name. These columns can also have a blank value.

The following is an example output of svn status command, which also shows whether a particular file is locked or not.

c:\> svn status
- M - - - -  	index.html
! - - - - C  	css/style.css
- - L - - -  	images/sprites.jpg
A - - - - C 	js/project.js

In the above output, we see the following status codes for the files.

  1. M in second column indicates that properties of this file has been modified.
  2. ! in first column indicated that the file is missing.
  3. C in sixth column indicates that this file is in conflicting state.
  4. L in third column indicates that this file is locked.
  5. A in first column indicates that the file is scheduled for addition.

2. View More Info about a Locked File

Use svn info command to view detailed information about a particular file as shown below.

c:\>svn info sprites.jpg
Path: sprites.jpg
Name: sprites.jpg
Working Copy Root Path: C:\
URL: https://thegeekstuff.com/code_base/trunk
Relative URL: ^/trunk/webroot/Site
Repository Root: https://thegeekstuff.com/scm/code
Repository UUID: d56a3af8-c092-443f-9987-f536df04eac3
Revision: 6704
Node Kind: file
Schedule: normal
Last Changed Author: tgs
Last Changed Rev: 5909
Last Changed Date: 2014-06-26 20:14:49 +0530 (Thu, 26 Jun 2014)
Text Last Updated: 2014-07-05 16:54:30 +0530 (Sat, 05 Jul 2014)
Checksum: a4ee994c5938bfaa9802270b8fc1051e
Lock Token: opaquelocktoken:187ef87c-0437-11e4-bec9-a9
Lock Owner: tgs
Lock Created: 2014-07-05 16:55:40 +0530 (Sat, 05 Jul 2014)

3. Lock a File using SVN Lock

Use this command when you are working on a binary file and you like to indicate others that you are working on it.

The following command locks the tgs-logo.jpg binary file.

c:\>svn lock tgs-logo.jpg
'tgs-logo.jpg' locked by user 'tgs'

4. Unlock a File using SVN Unlock

After you are done working on a file, or if you decide not to work on it anymore, you can simply unlock the file to indicate others that they can start to modify that file.

The following command unlocks the tgs-logo.jpg binary file.

c:\>svn unlock tgs-logo.jpg
'tgs-logo.jpg' unlocked

5. Status K of a File

After you lock a file, when you execute the svn satus command, you’ll see the status “K”, which indicates that the file is locked in this particular working copy.

c:\>svn lock tgs-logo.jpg
'tgs-logo.jpg' locked by user 'tgs'

c:\>svn status tgs-logo.jpg
	K  tgs-logo.jpg

6. Others Can’t Commit a Locked File

When some other users tries to commit a file that is locked by you, they will get the following message saying that file is locked.

c:\>svn commit -m “some modification done” tgs-logo.jpg
Sending        tgs-logo.jpg
svn: Commit failed (details follow):
svn: Server sent unexpected return value (423 Locked)

The other user can run “svn status” and “svn info” command to view more information about this locked file as shown below.

c:\>svn status tgs-logo.jpg
- - L - - -  	images/tgs-logo.jpg

c:\>svn info tgs-logo.jpg
Path: tgs-logo.jpg
Name: tgs-logo.jpg
Working Copy Root Path: C:\
URL: https://thegeekstuff.com/code_base/trunk
Relative URL: ^/trunk/webroot/Site
Repository Root: https://thegeekstuff.com/scm/code
Repository UUID: d56a3af8-c092-443f-9987-f536df04eac3
Revision: 6704
Node Kind: file
Schedule: normal
Last Changed Author: tgs
Last Changed Rev: 5909
Last Changed Date: 2014-06-26 20:14:49 +0530 (Thu, 26 Jun 2014)
Text Last Updated: 2014-07-05 16:54:30 +0530 (Sat, 05 Jul 2014)
Checksum: a4ee994c5938bfaa9802270b8fc1051e
Lock Token: opaquelocktoken:187ef87c-0437-11e4-bec9-a9
Lock Owner: tgs
Lock Created: 2014-07-05 16:55:40 +0530 (Sat, 05 Jul 2014)

7. Force Unlock SVN Locks

If some other users wants to still commit the file, they have to break the lock using “svan unlock force” as shown below.

c:\> svn unlock tgs-logo.jpg
svn: 'tgs-logo.jpg' is not locked in this working copy

c:\> svn info tgs-logo.jpg | grep URL
URL: https://thegeekstuff.com/code_base/trunk/tgs-logo.jpg

c:\> svn unlock https://thegeekstuff.com/code_base/trunk/tgs-logo.jpg
svn: Unlock request failed: 403 Forbidden

c:\> svn unlock --force https://thegeekstuff.com/code_base/trunk/tgs-logo.jpg
'tgs-logo.jpg' unlocked.

In the above example the following is happening in each step:

  1. First, the user tries to unlock the file because his previous commit failed. Then, he finds that it is not locked in his working copy.
  2. So, he then uses the grep command pipe-lined with svn info command to get the repository url.
  3. He again tries to unlock it with the absolute url but he fails again.
  4. Finally, he uses the parameter ‘–force’ to break the lock belonging to other user.

8. After Force Unlock on a File

Now, if the user tgs comes and runs the command svn status, it will show as it is locked in current working copy as shown below.

c:\> svn status tgs-logo.jpg
     K  tgs-logo.jpg

But, if the user tgs runs the command svn status -u it will display the letter B, which indicates that the lock has been broken as shown below.

c:\> svn status -u tgs-logo.jpg
     B        tgs-logo.jpg
Status against revision:     5909.

Now, running an svn update will clear the token of the Broken lock and then running svn status will return blank result.

c:\> svn update tgs-logo.jpg
 B  	tgs-logo.jpg
Updated to revision 5909.

c:\> svn status tgs-logo.jpg
- - - - - - - - - - - - - -

9. Stealing SVN Locks using Force Lock

Now if the other users not only want to break the lock, but also want to lock it for themselves then they can pass the parameter ‘–force’ to the ‘svn lock’ command to lock it, as shown below.

c:\> svn lock tgs-logo.jpg
svn: Lock request failed: 423 Locked ( http://thegeekstuff.com)

c:\> svn lock --force tgs-logo.jpg
'tgs-logo.jpg' locked by user 'tgsOther'

This time, when the user tgs runs the command svn status -u, it will display letter T, indicating that lock has been stolen as shown below.

c:\> svn status -u tgs-logo.jpg
     T        tgs-logo.jpg
Status against revision:   5909.

Linux Sysadmin Course Linux provides several powerful administrative tools and utilities which will help you to manage your systems effectively. If you don’t know what these tools are and how to use them, you could be spending lot of time trying to perform even the basic administrative tasks. The focus of this course is to help you understand system administration tools, which will help you to become an effective Linux system administrator.
Get the Linux Sysadmin Course Now!

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

{ 2 comments… read them below or add one }

1 Neil July 30, 2014 at 3:34 am

Just a little note to say the link ‘merge svn branch and trunk’ says ‘merge svn branch and truck’

2 Ramesh Natarajan July 31, 2014 at 3:58 pm

@Neil,

Thanks for pointing out the typo. It is fixed now.

Leave a Comment

Previous post:

Next post: