How do I "switch" with Mercurial

43,699

Solution 1

hg update -r<REV>

The update command will switch to a specified revision.

Solution 2

The switch command does two things in Subversion:

  1. Update the working copy to mirror a new URL within the repository. This behaviour is similar to 'svn update', and is the way to move a working copy to a branch or tag within the same repository.

  2. Rewrite working copy URL metadata to reflect a syntactic change only. This is used when repository's root URL changes (such as a scheme or hostname change) but your working copy still reflects the same directory within the same repository.

The hg update foo command matches the former most closely if you switch from, say, /trunk to /branches/foo in Subversion.

If you want to do the latter, then simply edit .hg/hgrc. You'll find a [paths] section that looks like this:

[paths]
default = http://example.net/something

and you can change this default push/pull URL to something else as you see fit. You can also add other entries to this section if you often need to push/pull from other locations. If you want to pull from one location, but push to another, then add a default-push entry.

Solution 3

I you want to switch branches, do the following:

First check what branches are available, type hg branches. This will list all the available branches.

Next determine which branch you are currently working on, type hg branch. This will tell you what is your current branch.

Finally to switch a branch (and wipe all local changes), type hg update -C name-of-branch. This will switch you to the branch name you specified.

Verify by typing hg branch again.

Solution 4

To switch to a remote branch 'tip':

hg pull -u <remote_branch>

For local branch (already up-to-date) just:

hg update -r <local_branch>
Share:
43,699
Jonas Meyer
Author by

Jonas Meyer

Updated on October 25, 2020

Comments

  • Jonas Meyer
    Jonas Meyer over 3 years

    How do I do what svn switch does, in Mercurial?

    That is change my working directory to switch to another branch in the repository?

  • quark
    quark almost 15 years
    Just a note: if you use named (internal) branches, you can switch to the branch name with -r also: 'hg update -r <branch>'
  • Martin Geisler
    Martin Geisler almost 13 years
    I find it easier to add another path to the [paths] section in the .hg/hgrc file, or simply change the default path listed there. See hg help paths for more info.
  • The Coder
    The Coder almost 12 years
    Note even if you exclude the -C clean option any local uncommitted changes will be removed when doing this. If you want to keep uncommitted changes shelve them first :o)
  • andybuckley
    andybuckley almost 11 years
    I think this needs to be hg update -C <branch>, not -r. As @Danmar says in his answer, the hg branches and hg branch commands are also useful.
  • koustubh
    koustubh over 10 years
    Beginner mistake, be sure you've done hg pull first (so that you have the branch locally to switch to)