Last Updated: 21 Nov 2020

   |   

Author: (external edit)

Setting the $Id$ Tag in Subversion

CVS will automatically set an $Id$ tag in each file; it looks something like this:

$Id: test.php 110 2009-04-28 05:20:41Z dordal $

This is super-helpful for two reasons:

  • it lets you know at a glance what revision of a file you're working with, and who last worked on it
  • if you export your code (say as part of a deployment process), it lets others know what revision of a file they have

Subversion, by default, doesn't add these tags. However, you can configure it to set the tag when you checkin a file.

Note that this configuration has to be done client side; unfortunately you can't set this on the svn server for all clients. If you have multiple developers, you'll need to be pretty strict about making sure they all go through this procedure.

Configure subversion to automatically add the $Id: $ tag

First, update your ~/.subversion/config with the following info:

[miscellany]
enable-auto-props = yes

[auto-props]
*.php = svn:keywords=Id
*.js = svn:keywords=Id

NOTE: You may want to add additional file types to the list above. As it stands, it will only set $Id: $ tags for *.php and *.js.

Set props on existing files

That configuration will automatically apply the svn:keywords property (which sets the $Id$ tag) to all new files. But what about existing files? For these, you'll have to set the property manually. Go to the root of your source tree, and run this command:

find . \( -name "*.php" -o -name "*.js" \) -exec svn propset svn:keywords Id {} \;

find will return a list of all .php and .js files, and then run svn propset on them to set the appropriate svn:keywords property. Now, all files have the property set, and will automatically set the $Id$ tag on checkin.

Add the $Id$ tag to each file

Finally, go through each file and add the prototype $Id$ tag. I like to do this in the comments at the header of each file, e.g.:

/*
 * @author David Ordal, david -at- ordal.com
 * @version $Id$
 *
 */

When you checkin the file, you'll see it expand to:

/*
 * @author David Ordal, david -at- ordal.com
 * @version $Id: test.php 110 2009-04-28 05:20:41Z dordal $
 *
 */

That's it!

Discussion

Tamlyn, Mar 19, 2010 03:36 PM

Nice one, thanks. I always forget exactly how to get this set up.

Daniel Leblond, Aug 5, 2010 03:57 PM

Thanks for this quick lesson, very helpful.

Rich W, Jul 21, 2011 04:25 PM

There's more tags to set than just the ID too. You have to set each one up in the ~/.subversion/config file, but here's the subversion guide to using them:

http://svnbook.red-bean.com/en/1.4/svn.advanced.props.special.keywords.html

Jay Burrill, Aug 10, 2011 06:03 PM

Thanks for the information. For the Windows users, the same config file would be found in %USERPROFILE%\Application Data\Subversion\config

Carrieann, Nov 25, 2011 02:50 AM

That's really tihknnig out of the box. Thanks!

Makailee, Nov 27, 2011 02:51 AM

I was really cofnused, and this answered all my questions.

test220.227.8.195, Feb 13, 2015 07:51 AM

Great information!

Also ,I want to enable revisions logging in my file so that it look below snippet whenever I commit my code to SVN:
/*

  • @author David Ordal, david -at- ordal.com
  • @version $Id: test.php 3 2009-04-29 04:20:41Z dordal $
  • @version $Id: test.php 2 2009-04-28 02:20:41Z dordal $
  • @version $Id: test.php 1 2009-04-27 01:20:41Z dordal $
  • /

Is it possible? Thanks in advance!

Enter your comment. Wiki syntax is allowed: