Up: Version Control

The Command Line

slide 001 Hello, and welcome to the fifth episode of the Software Carpentry lecture on version control. In this episode, we will explain how to work with a version control system from the command line.
slide 002 In the previous episodes, we showed you how to use the SmartSVN GUI to interact with your repository.
slide 003 We will now show you how to accomplish these same tasks using commands from the shell, instead.
slide 004 Running commands from the shell is faster than using SmartSVN,
slide 005 And it is simple and not cluttered, unlike many GUIs.
slide 006 In the prior episodes, Dracula’s first task was to check out a working copy of the repository.
slide 007 To do this from the command line, we use the “svn checkout” command…
slide 008 …or just “svn co” for short.
slide 009 The checkout command takes two arguments.
slide 010 The first argument is the location of the repository.
slide 011 The second argument is the location for the working copy on your computer.
slide 012 When the command is executed, the files in the repository are copied to Dracula’s computer.
slide 013 He can then navigate to the working copy…
slide 014 …and use the normal shell commands to view the files.
slide 015 Once inside the working directory, the history of the project can be viewed using the “svn log” command…
slide 016 …which gives a list of changes, and their details.
slide 017 We see the comments…
slide 018 …revision numbers…
slide 019 …and the names of the editors, among other things.
slide 020 Suppose Dracula now goes to the Jupiter folder,
slide 021 and creates a new file called “moons.txt”.
slide 022 (For details about file creation, please see the Software Carpentry lecture on the shell.)
slide 023 After saving the file, Dracula wants to add it to the repository.
slide 024 First, he uses the “svn add” command…
slide 025 …with the location of the file as an argument.
slide 026 This tells subversion to start keeping track of changes to the file.
slide 027 Next, Dracula needs to commit the changes to the repository.
slide 028 This is accomplished using the “svn commit” command.
slide 029 To give a comment for the revision, the -m flag is used…
slide 030 …followed by the comment, in quotation marks.
slide 031 When Dracula executes the command, moons.txt is committed to the master copy of the repository. We are now on revision 7.
slide 032 Wolfman, and any other users, will now need to update their working copies, since they are no longer at the latest revision.
slide 033 Wolfman navigates to his working copy of the repository…
slide 034 …and uses the “svn update” command.
slide 035 Wolfman now has an up-to-date working copy.
slide 036 Now that Wolfman has updated to the latest revision, he takes a look at moons.txt
slide 037 …and notices a spelling error.
slide 038 He corrects the error…
slide 039 …and also adds additional information about Amalthea.
slide 040 Wolfman then commits his changes.
slide 041 Later on, Dracula navigates to his working copy, and wants to see the changes that Wolfman has made to the repository.
slide 042 To view such changes, we use “svn diff”, which displays differences between revisions
slide 043 Specifically, to compare the master copy to the working copy, Dracula uses svn diff, with the -r flag. The -r flag indicates which revision we want to compare with the working copy.
slide 044 To compare with the most recent revision, we pass the parameter “HEAD”
slide 045 Execution of the command shows that the working version of moons.txt is different from the master version, and tells us which lines are different.
slide 046 A minus sign at the beginning of a line indicates that the line is present in the master copy, but is missing in our local copy.
slide 047 A plus sign indicates that the line is in the local copy, but not in the master copy.
slide 048 Now that Dracula has reviewed the changes, he updates to the latest revision.
slide 049 Both Wolfman and Dracula are up-to-date with the master copy, at revision 8.
slide 050 Next, we will show you how to deal with conflicts from the command line.
slide 051 Suppose Dracula and Wolfman are editing different lines of their local copies, but at the same time.
slide 052 Dracula commits, and the repository is now at revision 9.
slide 053 When Wolfman tries to commit, he gets an error: Commit failed. File or directory “moons.txt” is out of date. Try updating.
slide 054 This error occurs because Wolfman is at revision 8, but the master copy is at revision 9.
slide 055 To resolve this, Wolfman updates to revision 9. Since Dracula did not edit the same lines, the changes are merged without complaint.
slide 056 Wolfman can now commit his changes.
slide 057 Now, both Wolfman and Dracula add measurement units to the file.
slide 058 Wolfman commits his changes, bringing us to revision 11.
slide 059 When Dracula tries to commit, he gets an error, since he is not at the latest revision
slide 060 He tries to update
slide 061 But there is a conflict discovered in moons.txt, since he edited the same line as Wolfman.
slide 062 To view the conflict, Dracula selects “diff full” by typing “df”
slide 063 We get the following output
slide 064 Between the less the row of less than signs, and the row of equals signs, we see what is in Dracula’s local copy,
slide 065 Between the equals signs and greater than signs, we see what is in the master copy.
slide 066 Now that we’ve identified the conflict, Dracula selects 'e' to edit the file.
slide 067 This opens up a text editor.
slide 068 In SmartSVN, we had a graphical tool for resolving conflicts.
slide 069 We do not have this option in the shell.
slide 070 Instead, Dracula must fix the conflict manually, through regular text editing. He corrects the conflict and saves the file…
slide 071 …and is once again presented with a list of options.
slide 072 This time Dracula selects 'r' for “resolved”
slide 073 …and the update completes.
slide 074 Dracula is now able to commit his changes.
slide 075 Next, we will take about reversing changes.
slide 076 Suppose Wolfman is up-to-date, at revision 12
slide 077 He makes some changes to moons.txt
slide 078 But later realizes he wants to undo these changes.
slide 079 Since he hasn’t committed his changes, he simply needs to revert his local copy
slide 080 To throw away local changes, the “svn revert” command is used…
slide 081 …with the name of the file as an argument
slide 082 This will undo all the changes made to moons.txt
slide 083 Suppose now that Dracula is up-to-date at revision 12.
slide 084 He makes changes to moons.txt…
slide 085 …and commits, creating revision 13.
slide 086 He later decides that he wants to reverse these changes.
slide 087 Revisions cannot be erased from the repository,
slide 088 So instead, Dracula needs to copy revision 12 forward, merging with revision 13, to create a new revision.
slide 089 Dracula uses the “svn merge” command
slide 090 The -r flag specifies the range of revisions we are merging
slide 091 To undo the changes that were made from revision 12 to revision 13, we write “HEAD” colon 12, since we are going backward in time from the most recent revision, to revision 12.
slide 092 And then specify the file we are merging.
slide 093 Dracula’s local copy of moons.txt is now back to what it looked like in revision 12.
slide 094 He uses “svn diff” to verify that the changes have, indeed, been removed
slide 095 He then commits, creating revision 14. The changes to moons.txt have been reversed.
slide 096 To summarize, here is a list of the subversion commands that were covered in this episode.
slide 097

  1. October 6th, 2010 at 04:27 | #1

    Nice job. I normally use Subversion with TortoiseSVN — the server is FreeBSD, and the clients are always Windows. But I went through your slides in the terminal of an Ubuntu virtual machine and it was a very pleasant experience. Thank you for posting this and for having the slides with notes in addition to the video.

  2. Nick
    October 14th, 2010 at 13:27 | #2

    I would like to work on several different computers. All have internet access, but none are “networked”. Where would you put the repository and how would you access it from any computer you happen to be on? I was thinking about putting the repository on a portable hard drive and just carrying it around with me (will this work???) Any suggesstions are welcome.

  3. Ainsley
    October 14th, 2010 at 16:10 | #3

    @Nick
    Hi Nick! I’ve answered your question in the “FAQ” section on the main Version Control page, located here.

  4. Terri Yu
    January 21st, 2011 at 16:29 | #4

    The one tricky thing I found out was that when you try to do a reverse merge on the command line you have to put the numbers in decreasing order. For example, to reverse merge your current copy of a file back to revision 2, you should type “svn merge -r HEAD:2″.

  1. No trackbacks yet.