Size: 31533
Comment:
|
Size: 31405
Comment:
|
Deletions are marked like this. | Additions are marked like this. |
Line 3: | Line 3: |
With Mercurial you can use a multitude of different workflows. This page shows some of them, including their use cases. It is intended to make it easy for beginners of version tracking to get going instantly and learn completely incrementally. It doesn't explain the concepts used, because there are already many other great resources doing that, for example [http://www.selenic.com/mercurial/wiki/UnderstandingMercurial the wiki] and [http://hgbook.red-bean.com/read/behind-the-scenes.html the hgbook]. | With Mercurial you can use a multitude of different workflows. This page shows some of them, including their use cases. It is intended to make it easy for beginners of version tracking to get going instantly and learn completely incrementally. It doesn't explain the concepts used, because there are already many other great resources doing that, for example ([[Základy Mercurialu]] a [[http://hgbook.red-bean.com/read/behind-the-scenes.html|Za kulisami]]). |
Line 7: | Line 7: |
<div class="note"> Note: <!> This guide doesn't require any prior knowledge of version control systems (though subversion users will likely feel at home quite quickly). Basic command line abilities are helpful, because we'll use the command line client. </div> |
<!> This guide doesn't require any prior knowledge of version control systems (though subversion users will likely feel at home quite quickly). Basic command line abilities are helpful, because we'll use the command line client. |
Line 49: | Line 45: |
<div class="note"> Note: You can also go into an existing directory with files and init the repository there. </div> |
<!> You can also go into an existing directory with files and init the repository there. {{{ |
Line 57: | Line 49: |
}}} | |
Line 60: | Line 52: |
{{{ | |
Line 62: | Line 54: |
}}} | |
Line 65: | Line 57: |
{{{ | |
Line 67: | Line 59: |
}}} | |
Line 70: | Line 62: |
{{{ | |
Line 72: | Line 64: |
}}} | |
Line 75: | Line 67: |
{{{ | |
Line 77: | Line 69: |
}}} | |
Line 80: | Line 72: |
$ hg commit |
{{{ $ hg commit }}} |
Line 86: | Line 78: |
<div class="note"> Note: You can also supply the commit message directly via ''hg commit -m 'MESSAGE'''. </div> |
<!> You can also supply the commit message directly via ''hg commit -m 'MESSAGE'''. |
Line 97: | Line 85: |
{{{ | |
Line 104: | Line 92: |
}}} | |
Line 108: | Line 96: |
<div class="note"> Note: Should you forget to do the explicit copy or move, you can still tell Mercurial to detect the changes via ''hg addremove --similarity 100''. Just use ''hg help addremove'' for details. </div> |
Should you forget to do the explicit copy or move, you can still tell Mercurial to detect the changes via ''hg addremove --similarity 100''. Just use ''hg help addremove'' for details. |
Line 115: | Line 99: |
{{{ | |
Line 117: | Line 101: |
}}} | |
Line 122: | Line 106: |
{{{ | |
Line 124: | Line 108: |
}}} | |
Line 143: | Line 127: |
{{{ | |
Line 154: | Line 138: |
}}} |
Learning Mercurial in Workflows
With Mercurial you can use a multitude of different workflows. This page shows some of them, including their use cases. It is intended to make it easy for beginners of version tracking to get going instantly and learn completely incrementally. It doesn't explain the concepts used, because there are already many other great resources doing that, for example (Základy Mercurialu a Za kulisami).
If you want a more exhaustive tutorial with the basics, please have a look at the [http://www.selenic.com/mercurial/wiki/Tutorial Tutorial in the Mercurial Wiki]. For a really detailed and very nice to read description of Mercurial, please have a look at [http://hgbook.red-bean.com/ Mercurial: The Definitive Guide].
This guide doesn't require any prior knowledge of version control systems (though subversion users will likely feel at home quite quickly). Basic command line abilities are helpful, because we'll use the command line client.
Basic workflows
We go from simple to more complex workflows. Those further down build on previous workflows.
Log keeping
Use Case
The first workflow is also the easiest one: You want to use Mercurial to be able to look back when you did which changes.
This workflow only requires an installed Mercurial and write access to some file storage (you almost definitely have that ). It shows the basic techniques for more complex workflows.
Workflow
Prepare Mercurial
As first step, you should teach Mercurial your name. For that you open the file ~/.hgrc with a text-editor and add the ui section (user interaction) with your username:
[ui] username = Mr. Johnson
Initialize the project
Now you add a new folder in which you want to work:
$ hg init project
Add files and track them
$ cd project $ (add files) $ hg add
You can also go into an existing directory with files and init the repository there.
$ cd project $ hg init
Alternatively you can add only specific files instead of all files in the directory. Mercurial will then track only these files and won't know about the others. The following tells mercurial to track all files whose names begin with "file0" as well as file10, file11 and file12.
$ hg add file0* file10 file11 file12
Save changes
$ (do some changes)
see which files changed, which have been added or removed, and which aren't tracked yet
$ hg status
see the exact changes
$ hg diff
commit the changes.
$ hg commit
now an editor pops up and asks you for a commit message. Upon saving and closing the editor, your changes have been stored by Mercurial.
When you copy or move files, you should tell Mercurial to do the copy or move for you, so it can track the relationship between the files. Remember to Now you have two files, "copy" and "target", and Mercurial knows how they are related. Should you forget to do the explicit copy or move, you can still tell Mercurial to detect the changes via
This prints a list of changesets along with their date, the user who committed them (you) and their commit message. To see a certain revision, you can use the
The second workflow is still very easy: You're a lone developer and you want to use Mercurial to keep track of your own changes. It works just like the log keeping workflow, with the difference that you go back to earlier changes at times. To start a new project, you initialize a repository, add your files and commit whenever you finished a part of your work. Also you check your history from time to time, so see how you progressed.
Init your project, add files, see changes and commit them.
Different from the log keeping workflow, you'll want to go back in history at times and do some changes directly there, for example because an earlier change introduced a bug and you want to fix it where it occurred. To look at a previous version of your code, you can use update. Let's assume that you want to see revision 3. Now your code is back at revision 3, the fourth commit (Mercurial starts counting at 0). To check if you're really at that revision, you can use <div class="note"> Note: To update to the most recent revision, you can use "tip" as revision name. <div class="note"> Note: If at any place any command complains, your best bet is to read what it tells you and follow that advice. </div><div class="note"> Note: Instead of Note: To get a revision devoid of files, just
When you find a bug in some earlier revision you have two options: either you can fix it in the current code, or you can go back in history and fix the code exactly where you did it, which creates a cleaner history. To do it the cleaner way, you first update to the old revision, fix the bug and commit it. Afterwards you merge this revision and commit the merge. Don't worry, though: Merging in mercurial is fast and painless, as you'll see in an instant. Let's assume the bug was introduced in revision 3. Now the fix is already stored in history. We just need to merge it with the current version of your code. If there are conflicts use First list the files with conflicts Then resolve them one by one. Mark the fixed file as resolved Commit the merge, as soon as you resolved all conflicts. This step is also necessary when there were no conflicts! At this point, your fix is merged with all your other work, and you can just go on coding. Additionally the history shows clearly where you fixed the bug, so you'll always be able to check where the bug was. <div class="note"> Note: Most merges will just work. You only need So now you can initialize repositories, save changes, update to previous changes and develop in a nonlinear history by committing in earlier changesets and merging the changes into the current code. <div class="note"> Note: If you fix a bug in an earlier revision, and some later revision copied or moved that file, the fix will be propagated to the target file(s) when you merge. This is the main reason why you should always use
At times you'll be working on several features in parallel. If you want to avoid mixing incomplete code versions, you can create clones of your local repository and work on each feature in its own code directory. After finishing your feature you then
First create the feature clone and do some changes Now check what will come in when you <div class="note"> Note: If you want to see the diffs, you can use If you like the changes, you pull them into the project Now you have the history of feature1 inside your project, but the changes aren't yet visible. Instead they are only stored inside a ".hg" directory of the project ([http://hgbook.red-bean.com/read/behind-the-scenes.html more information on the store]). <div class="note"> Note: From now on we'll use the name "repository" for a directory which has a .hg directory with Mercurial history. </div> If you didn't do any changes in the project, while you were working on feature1, you can just update to tip ( Merge feature1 into the project code If there are conflicts use You can create an arbitrary number of clones and also carry them around on USB sticks. Also you can use them to synchronize your files at home and at work, or between your desktop and your laptop. <div class="note"> Note: You also have to commit after a merge when there are no conflicts, because merging creates new history and you might want to attach a specific message to the merge (like "merge feature1"). </div>
Now you can work on different features in parallel, but from time to time a bad commit might sneak in. Naturally you could then just go back one revision and merge the stray error, keeping all mistakes out of the merged revision. However, there's an easier way, if you realize your error before you do another Rolling back means undoing the last operation which added something to your history. Imagine you just realized that you did a bad commit - for example you didn't see a spelling error in a label. To fix it you would use And then redo the commit If you can use the command history of your shell and you added the previous message via Though it changes your history, rolling back doesn't change your files. It only undoes the last addition to your history. But beware, that a rollback itself can't be undone. If you <div class="note"> Note: Rollback is possible, because Mercurial uses transactions when recording changes, and you can use the transaction record to undo the last transaction. This means that you can also use
Now we go one step further: You are no longer alone, and you want to share your changes with others and include their changes. The basic requirement for that is that you have to be able to see the changes of others. Mercurial allows you to do that very easily by including a simple webserver from which you can <div class="note"> Note: There are a few other ways to share changes, though. Instead of using the builtin webserver, you can also send the changes by email or setup a shared repository, to where you
This is the easiest way to quickly share changes. First the one who wants to share his changes creates the webserver Now all others can point their browsers to his IP address (for example 192.168.178.100) at port 8000. They will then see all his history there and can decide if they want to pull his changes. $ firefox http://192.168.178.100:8000 If they decide to include the changes, they just pull from the same URL $ hg pull http://192.168.178.100:8000 At this point you all can work as if you had pulled from a local repository. All the data is now in your individual repositories and you can merge the changes and work with them without needing any connection to the served repository.
Often you won't have direct access to the repository of someone else, be it because he's behind a restrictive firewall, or because you live in different timezones. You might also want to keep your changes confidential and prefer internal email (if you want additional protection, you can also encrypt the emails, for example with [http://gnupg.org GnuPG]). In that case, you can easily export your changes as patches and send them by email. Another reason to send them by email can be that your policy requires manual review of the changes when the other developers are used to reading diffs in emails. I'm sure you can think of more reasons. Sending the changes via email is pretty straightforward with Mercurial. You just First check which changes you want to export We assume that you want to export changeset 3 and 4 $ hg export 3 > change3.diff $ hg export 4 > change4.diff Now attach them to an email and your colleagues can just run To be careful, they first That's it. They can now test your changes in feature clones. If they accept them, they <div class="note"> Note: The Note: </div>
Sending changes by email might be the easiest way to reach people when you aren't yet part of the regular development team, but it creates additional workload: You have to Till now we transferred all changes either via email or via But to make use of it, we first need something we can push to. By default If you want to use an existing shared server, you can use Otherwise you first need to setup a BitBucket Account. Just signup at [http://bitbucket.org BitBucket]. After signing up (and login) hover your mouse over "Repositories". There click the item at the bottom of the opening dialog which say "Create new". Give it a name and a description. If you want to keep it hidden from the public, select "private" $ firefox http://bitbucket.org Now your repository is created and you see instructions for $ hg push https://bitbucket.org/ArneBab/hello/ (Replace the URL with the URL of your created repository. If your username is "Foo" and your repository is named "bar", the URL will be https://bitbucket.org/Foo/bar/) Mercurial will ask for your BitBucket name and password, then Voili?1, your code is online. <div class="note"> Note: You can also [http://bitbucket.org/help/UsingSSH use SSH for pushing to BitBucket]. </div> Now it's time to tell all your colleagues to sign up at BitBucket, too. After that you can click the "Admin" tab of your created repository and add the usernames of your colleagues on the right side under "Permission: Writers". Now they are allowed to (If you chose to make the repository private, you'll need to add them to "Permission: Readers", too) If one of you now wants to publish changes, he'll simply Publish your changes $ hg push https://bitbucket.org/ArneBab/hello/ Pull others changes into your local repository $ hg pull https://bitbucket.org/ArneBab/hello/ People who join you in development can also just $ hg clone https://bitbucket.org/ArneBab/hello/ hello That local repository will automatically be configured to pull/push from/to the online repository, so new contributors can just use <div class="note"> Note: To make this workflow more scalable, each one of you can have his own BitBucket repository and you can simply Note: You can also use this workflow with a shared server instead of BitBucket, either via SSH or via a shared directory. An example for an SSH URL with Mercurial is be ssh://user@example.com/path/to/repo. When using a shared directory you just push as if the repository in the shared directory were on your local drive. </div>
Now let's take a step back and look where we are. With the commands you already know, a bit reading of First this is good, because it means, that you can now use most of the concepts which are utilized in more complex workflows. Second it aids you, because convenience lets you focus on your task instead of focusing on your tool. It helps you concentrate on the coding itself. Still you can always go back to the basics, if you want to. A short summary of what you can do which can also act as a short check, if you still remember the meaning of the commands.
$ hg serve & $ cd .. $ hg clone http://127.0.0.1:8000 project-clone
$ hg export tip > ../changes.diff
$ hg pull http://127.0.0.1:8000
$ hg push https://bitbucket.org/USER/REPO (enter name and password in the prompt) $ hg pull https://bitbucket.org/USER/REPO Let's move on towards useful features and a bit more advanced workflows.
When you routinely pull code from others, it can happen that you overlook some bad change. As soon as others pull that change from you, you have little chance to get completely rid of it. To resolve that problem, Mercurial offers you the <div class="note"> Note: The basic commands don't directly rewrite history. If you want to do that, you need to activate some of the extensions which are shipped with mercurial. We'll come to that later on. </div>
Let's assume the bad change was revision 3, and you already have one more revision in your repository. To remove the bad code, you can just That's it. You reversed the bad change. It's still recorded that it was once there (following the principle "don't rewrite history, if it's not really necessary"), but it doesn't affect future code anymore.
Now that you can share changes and reverse them if necessary, you can go one step further: Using Mercurial to help in coordinating the coding. The first part is an easy way to develop features together, without requiring every developer to keep track of several feature clones.
When you want to split your development into several features, you need to keep track of who works on which feature and where to get which changes. Mercurial makes this easy for you by providing named branches. They are a part of the main repository, so they are available to everyone involved. At the same time, changes committed on a certain branch don't get mixed with the changes in the default branch, so features are kept separate, until they get merged into the default branch. <div class="note"> Note: Cloning a repository always puts you onto the default branch at first. </div>
When someone in your group wants to start coding on a feature without disturbing the others, he can create a named branch and commit there. When someone else wants to join in, he just updates to the branch and commits away. As soon as the feature is finished, someone merges the named branch into the default branch.
Create the branch Update into the branch and work in it Now you can
When you finished the feature, you And that's it. Now you can easily keep features separate without unnecessary bookkeeping. <div class="note"> Note: Named branches stay in history as permanent record after you finished your work. If you don't like having that record in your history, please have a look at some of the advanced [http://www.selenic.com/mercurial/wiki/Workflows workflows]. </div>
Since you can now code separate features more easily, you might want to mark certain revisions as fit for consumption (or similar). For example you might want to mark releases, or just mark off revisions as reviewed. For this Mercurial offers tags. Tags add a name to a revision and are part of the history. You can tag a change years after it was committed. The tag includes the information when it was added, and tags can be pulled, pushed and merged just like any other committed change. <div class="note"> Note: A tag must not contain the char ":", since that char is used for specifying multiple revisions - see "hg help revisions". </div><div class="note"> Note: To securely mark a revision, you can use the [http://www.selenic.com/mercurial/wiki/GpgExtension gpg extension] to sign the tag. </div>
Let's assume you want to give revision 3 the name "v0.1". Add the tag See all tags When you look at the log you'll now see a line in changeset 3 which marks the Tag. If someone wants to Now he'll be at the tagged revision and can work from there.
At times you will have changes in your repository, which you really don't want in it. There are many advanced options for removing these, and most of them use great extensions ([http://www.selenic.com/mercurial/wiki/MqExtension Mercurial Queues] is the most often used one), but in this basic guide, we'll solve the problem with just the commands we already learned. But we'll use an option to clone which we didn't yet use. This workflow becomes inconvenient when you need to remove changes, which are buried below many new changes. If you spot the bad changes early enough, you can get rid of them without too much effort, though.
Let's assume you want to get rid of revision 2 and the highest revision is 3. The first step is to use the "--rev" option to Now you can $ hg export 3 > ../changes.diff $ cd ../stripped $ hg import ../changes.diff If a part of the changes couldn't be applied, you'll see that part in *.rej files. If you have *.rej files, you'll have to include or discard changes by hand That's it. <div class="note"> Note: removing history will change the revision IDs of revisions after the removed one, and if you pull from someone else who still has the revision you removed, you will pull the removed parts again. That's why rewriting history should most times only be done for changes which you didn't yet publicise. </div>
So now you can work with Mercurial in private, and also share your changes in a multitude of ways. Additionally you can remove bad changes, either by creating a change in the repository which reverses the original change, or by really rewriting history, so it looks like the change never occurred. And you can separate the work on features in a single repository by using named branches and add tags to revisions which are visible markers for others and can be used to update to the tagged revisions. With this we can conclude our practical guide.
If you now want to check some more complex workflows, please have a look at the general [http://selenic.com/mercurial/wiki/Workflows workflows wikipage]. To deepen your understanding, you should also check the [http://mercurial.selenic.com/wiki/UnderstandingMercurial basic concept overview]. Have fun with Mercurial! *PS: This guide is licensed under the [http://gnu.org/licenses/gpl-2.0.html GPLv2]* *PPS: Written by Arne Babenhauserheide, edited by David Soria Parra, Nicolas Dumazet and Steve Losh. Conversion from HTML http://labs.seapine.com/htmltowiki.cgi* You can also supply the commit message directly via hg commit -m 'MESSAGE.
Move and copy files
$ hg cp original copy
$ hg commit
(enter the commit message)
$ hg mv original target
$ hg commit
(enter the commit message)
hg addremove --similarity 100. Just use hg help addremove for details. Check your history
$ hg log
$ hg log -p -r 3
Lone developer with nonlinear history
Use case
Workflow
Basics from log keeping
$ hg init project
$ cd project
$ (add files)
$ hg add # tell Mercurial to track all files
$ (do some changes)
$ hg diff # see changes
$ hg commit # save changes
$ hg cp # copy files or folders
$ hg mv # move files or folders
$ hg log # see history
Seeing an earlier revision
hg update you can also use the shorthand hg up. Similarly you can abbreviate hg commit to hg ci. </div><div class="note">
update to "null" via hg update null. That's the revision before any files were added. </div> Fixing errors in earlier revisions
resolve, when merge complains. </div>
hg cp and hg mv. </div> Separate features
Use Case
Workflow
Work in different clones
hg incoming --patch just as you can do with hg log --patch for the changes in the repository. </div> Rollback mistakes
rollback to undo your last pull, if you didn't yet commit anything new. </div> Sharing changes
Use Case
push changes instead of pulling them. We'll cover one of those later. </div> Workflow
Using the builtin webserver
Sending changes by email
patchbomb extension automates the email-sending, but you don't need it for this workflow. </div><div class="note"> Using a shared repository
pull from the others repositories. That way you can easily establish workflows in which certain people act as integrators and finally push checked code to a shared pull repository from which all others pull. </div><div class="note"> Summary
create a project
do nonlinear development
use feature clones
share your repository via the integrated webserver
export changes to files
import changes from files
pull changes from a served repository (hg serve still runs on project)
Use shared repositories on BitBucket
Advanced workflows
Backing out bad revisions
Use Case
Workflow
Collaborative feature development
Use Case
Workflow
Working in a named branch
Merge the named branch
Tagging revisions
Use Case
Workflow
Removing history
Use Case
Workflow
Summary
More Complex Workflows