Plan for Software Carpentry 2012 Boot Camp on Version Control¶
About the session¶
The session is a two hour interactive live workshop, using EasyMercurial and the Mercurial command-line tool.
Its goal is to explain version control and introduce Mercurial to researchers who have never used it before, or who want to understand it better.
The basic plan is:
- Presentation introduction to version control in general
- Long worked example in which basic topics of version control are worked through using EasyMercurial and then some more advanced topics are returned to using the command-line tool
- Closing remarks talking about other tools, other topics of interest etc
Preliminary: Check that participants have EasyMercurial installed and working. All of the following exercises will be using EasyMercurial unless it says otherwise.
Part 1: Working by yourself¶
Topics: Initialising a repository, committing files, reading history, looking at diffs, reverting unwanted changes, going back in time to look at old versions
We will be working on a recipe for fish stew for a future recipe book.
Adding your first file¶
- Make a new directory, create a text file
fishstew.txtin it, start adding an ingredients list, save
- Run up EasyMercurial; enter user name and email address
- "Open" that directory, see
fishstew.txtin untracked file list: that means the version control system is not going to keep track of any changes unless we tell it to
- Add file: that tells the version control system to keep track of any future changes to it
- Commit: that sets in stone our having added the file. A commit is a checkpoint
- Supply a message, note that we now have some history
- Edit the file, change something, save it
- Note that the file is now marked as modified. (We might also see a backup file ending ~ or .bak from that editor -- ignore it for now, we'll come back to it in a moment)
- Each revision records the state of all files, not just one file: add another file,
omelette.txtand add that
- Commit change, note that we now have two revisions
- Review the history and look at the diff
Digression: every action we're taking here corresponds to one command-line command: show hg log, hg diff etc
- Go back to that backup file in My Work, add it to ignored list, commit
- The history is not just for information: we can go back to the previous version by updating to it...
- ... and then a normal update gets us back to the latest version again
Let's say this version is the one that we're going to send off to our agent, to see whether they can sell it to a publisher (or whatever we do in these modern times).
- Tag the current revision as v0.1 -- digression about sensible tag names on whiteboard?
- We can now identify this version easily in the history
- Make and commit another change, this one involving renaming a file
- What if we make a change and decide we don't want to commit it? Edit something, then hit Revert
Now we have history, but we are still in big trouble if our computer fails. Thus...
Part 2: Working by yourself, but "with backups"¶
Topics: Push, clone, using an online repo hosting service
Pushing local repository to a remote one¶
- Register an account on Bitbucket and create a new private repository
- Look up its URL
- In EasyMercurial, hit the Push button, enter URL, push to remote repo, check that the history is present and correct on the site
Synchronising new changes¶
- Make another change locally, commit (perhaps do this more than once before pushing)
- Push the change(s), and check the history on site again
Recovering from a disaster¶
- Exit EasyMercurial. Delete the local repository / working copy folder completely!
- Start EasyMercurial again, see that (sniff) the working copy is lost
- Clone it again from Bitbucket and note that the history is all there
Digression: Note command-line usage again, hg push, hg pull, hg clone: show a sequence of clones, modifying the last one and pushing back along the chain?
Part 3: Introducing other developers¶
Topics: Conflicts, merges, pull, annotate
Pair up and, in each pair, decide whose Bitbucket repo you will be working on and whose we'll just leave for now. (We should pair the "instructor" with someone as well).
Remark: in real-world use, this could very well be the same person just using two different computers
Getting someone else's changes¶
- The second person in the pair should then clone the repository from Bitbucket
- They should then change something and commit it
- First person pulls -- notes that there are no changes there (merely committing it doesn't put it on the server)
- Second person pushes
- First person pulls again -- the change is now in the history but not in the working copy
- First person updates -- the change is now in the working copy!
Making alternative versions¶
- Both people now make some edits: they should edit two different files and commit them
- The second user should push their changes to the remote repo first
- The first user then tries to push. They should get the "Push failed... The local repository may have been changed" message
- Then the first user pulls instead. (Perhaps checks the Incoming list first?)
- See that the history graph now shows two heads
Digression on sociological nature of conflict a la Greg if feeling expansive
Merging non-conflicting changes¶
- Hit Merge, see that the merge happens straight away
- Remark that this is the point at which you would now test the merged version
- Commit, push, get collaborator to pull
- Get each person to edit the same file, in conflicting ways
- Again, both users should try to push and the push should fail for the later one
- That user pulls, hits Merge, get the merge window up
- Do an "instructor-guided" merge
- Commit, push, get collaborator to pull
- Run annotate on the recipe file to see who changed what and when
Part 4: More sophisticated business at the command-line¶
hg archive: packaging from a tag¶
Having tagged the version (
v0.1 or whatever we called it) that we're going to send off to the agent, now we need to pull out only that version and send it off, without the whole repository attached.
- Open a terminal window
cdto the working copy
hg archive -r v0.1 book-v0.1.zip
book-v0.1.zipand make sure it contains (only) the correct files for revision 0.1
hg id: provenance when running experiments¶
hg idat command line and note that it shows the id of the current parent revision
- We can do this from a script when running an experiment, or from a Python program
hg bisect: finding the origin of bugs you can't see (analogy with
hg annotate for finding bugs you can see)¶
Things not yet incorporated into the above: Copying, renaming, deleting files; Branching and merging amonst branches; Stuff that is different in other systems
Should we cover (named) branches and merges between them? I think yes, if there is time.
Against: perhaps a level of complication too far for a two-hour intro; Greg doesn't cover them in the Subversion version; lessons learned from Hg are not immediately applicable to git or Subversion because the branching methods are somewhat different.
For: they are much simpler to use in Mercurial than in Subversion!
- Why is it a good idea to use version control on your projects?
- What is a version control repository?
- Suppose you’ve created a new file on your computer and you want to start using version control for it. How do you go about doing this?
- Jon is working on a version-controlled project with Ainsley and Tommy. He wakes up early one day, ready to do some work on the project. What is the first thing he should do?
- Tommy and Jon have up-to-date local repositories, and are both editing a file that contains 10 lines of data. Jon makes a change to the fifth line, and commits and pushes his changes to the remote repository they're both using. Tommy makes a change to the first line of the file, commits his changes, and tries to push them. What will happen, and what should Tommy do next?
- Ainsley and Jon are up-to-date, and are both editing the sixth line of a file. Jon commits and pushes his changes first. When Ainsley commits and tries to push, what will happen, and what should she do next?
- How do you undo local changes to files that have not been committed?
- Give the shell commands you would use to accomplish the following tasks:
- Check out the repository located at http://example.com/repo into the directory /cygwin/home/repo
- View the log of changes
- Add the file “experiment.txt” to the repository
- Commit the file to the repository.
- Update the local copy to reflect any new changes in the remote repository
What did not go well?
- Several technical problems:
- EasyMercurial installation on Linux -- many attendees were using Linux and easyhg (unlike hg) is not in the distro package repos
- diff/merge tool not always found correctly (again mostly Linux)
- I forgot to give the intro explaining what we were about to learn -- I explained the advantages of version control but I didn't say "we're using Mercurial, other systems are..." etc
- I should have explained revision hashes earlier
- I should have shown "backout" and command-line diffs (undoing something later)
- Audience was largely familiar with the command-line -- we were surprised by how familiar -- and might well have responded better to being taught the command-line tool
- I forgot to give the closing stuff about when you should put something in version control, how often you should commit, etc
- It was slightly undermined by having Greg then present stuff using Svn immediately afterward!