[Mercurial] Typical Mercurial Commands

mercurial_workflow_image

I’ve played around with three version control systems during my learning and working period. The first was CVS, where it was the first time I’ve touched a version-control mechanism during university. The second being Mercurial and lastly, Git; two of which I’ve learnt partially for supporting customers while I was working with Atlassian.

Now as I’m in a developer’s role, I would feel the need to pick up a version-control system as what most developers do. Thus, I re-learnt Mercurial once more.

There are a few basic commands that one should know and understand for a better understanding on how Mercurial works. Here’s a list of common commands that I’ve stumbled upon thus far.

    hg init

    • initialize the current directory to be a repo

    hg add %FILE_NAME%

    • add %FILE_NAME% to the repo

    hg commit -m "%YOUR_MESSAGE%"

    • confirms changes to the repo
    • -m argument: for adding a commit message to the commit

    hg clone

    • hg clone %REPO_URL% %NEW_FOLDER%
      • for cloning online repos
      • %REPO_URL% = repo URL
    • hg clone %CUR_REPO% %NEW_REPO%
      • for cloning existing local repo to a new one
      • %CUR_REPO% = repo to clone
      • %NEW_REPO% = destination for the clone repo

    hg parents or hg par

    • to view which revision is currently checked out

    hg log

    • shows a summary of every event that occurs in the repo:
                  $ hg log
                  changeset:   1:82e55d328c8c
                  tag:         tip
                  user:        mpm@selenic.com
                  date:        Fri Aug 26 01:21:28 2005 -0700
                  summary:     Create a makefile
                  changeset:   0:0a04b987be5a
                  user:        mpm@selenic.com
                  date:        Fri Aug 26 01:20:50 2005 -0700
                  summary:     Create a standard "hello, world" program
                  
    • changeset – identifies the changeset.
      • first number before the colon = revision number.
        • short identity of the changeset
        • only valid in the current repo
      • hexadecimal string after the colon = changeset ID
        • actual identity of the changeset
        • valid in all repos that contains this changeset
    • tag – symbolic name of the changeset.

      may contain tip (to identify the most recent changeset).

    • user – identifies the user who created this changeset.
    • date – local time of when this changeset was created.
    • summary – changeset description.
    • parent – identifies the parent changesets.

    hg log -v

    • detailed view of a repo’s history:
                  $ hg log -v
                  changeset:   1:82e55d328c8c
                  tag:         tip
                  user:        mpm@selenic.com
                  date:        Fri Aug 26 01:21:28 2005 -0700
                  files:       Makefile
                  description:
                  Create a makefile
                  (...)
                  
    • files – lists the modified files in this changeset.
    • description – multi-line description of the changeset.

    hg --debug

    • an even more detailed view of a repo’s history:
                  $ hg log --debug
                  changeset:   1:82e55d328c8ca4ee16520036c0aaace03a5beb65
                  tag:         tip
                  parent:      0:0a04b987be5ae354b710cefeba0e2d9de7ad41a9
                  parent:      -1:0000000000000000000000000000000000000000
                  manifest:    1:0c7c1d435e6703e03ac6634a7c32da3a082d1600
                  user:        mpm@selenic.com
                  date:        Fri Aug 26 01:21:28 2005 -0700
                  files+:      Makefile
                  extra:       branch=default
                  description:
                  Create a makefile
                  (...)
                  
    • changeset – now provides the unabbreviated changeset ID.
    • parent – now has 2:
      • changeset ID of both parents of this changeset.
      • -1:0000000000000000000000000000000000000000 refers to a non-existent parent.
    • manifest – provides the manifest ID for the changeset.

      manifest describes the contents of a repo at a particular changeset ID.

    • file+ – list files added to the changeset.
    • file- – list files removed to the changeset.

    hg log -r%CHANGESET_ID%

    • view specific changesets; specified with the %CHANGESET_ID%:
                  $ hg log -r1
                  changeset:   1:82e55d328c8c
                  tag:         tip
                  user:        mpm@selenic.com
                  date:        Fri Aug 26 01:21:28 2005 -0700
                  summary:     Create a makefile
                  

    hg log -r%CHANGESET_ID% -p

    • show patches associated with the changesets:
                  $ hg log -r1 -p
                  changeset:   1:82e55d328c8c
                  tag:         tip
                  user:        mpm@selenic.com
                  date:        Fri Aug 26 01:21:28 2005 -0700
                  summary:     Create a makefile
                  diff -r 0a04b987be5a -r 82e55d328c8c Makefile
                  --- /dev/null   Fri Aug 26 01:20:50 2005 -0700
                  +++ b/Makefile  Fri Aug 26 01:21:28 2005 -0700
                  @@ -0,0 +1,1 @@
                  +all: hello
                  

    hg tip or hg log -r tip

    • show info of the latest changeset:
                  $ hg tip
                  changeset:   1:82e55d328c8c
                  tag:         tip
                  user:        mpm@selenic.com
                  date:        Fri Aug 26 01:21:28 2005 -0700
                  summary:     Create a makefile
                  
    • adding the -q argument will just output the latest changeset’s ID:
                  $ hg -q tip
                  1:82e55d328c8c
                  

    hg status or hg st

    • shows the status of files in the repo.
    • a few denotations:
      Status Definition
      M modified
      ? untracked in repo (not added)

    hg diff

    • a detailed view of the actual changes made to files:
                  $ hg diff
                  diff -r 82e55d328c8c hello.c
                  --- a/hello.c   Fri Aug 26 01:21:28 2005 -0700
                  +++ b/hello.c   Mon May 05 00:27:56 2008 +0200
                  @@ -12,5 +12,6 @@
                   int main(int argc, char **argv)
                   {
                          printf("hello, world!\n");
                  +       printf("sure am glad I'm using Mercurial!\n");
                          return 0;
                   }
                  

    hg revert %FILE_NAME%

    • reverts %FILE_NAME% to its unmodified state.
    • to modify all files, replace %FILE_NAME% with --all.

    hg ci

    • creating a changeset.
    • enter any message(s) on the first line.

    hg pull

    • grab latest changesets that does not exist in the current repo.
    • can specify a path for a specific repo.

    hg update or hg up

    • updates the current repo with changes pulled from another.

Was That a Good Read? Rate It!

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

Add a Comment

Your email address will not be published. Required fields are marked *

Current day month ye@r *

CommentLuv badge