Hg command to get the file content without switching branch

Viewed 558

In HG, is there any command such that i can get the file content of the file from another branch without switching/checkout to the particular branch?

3 Answers

If you are using TortoiseHG, you can use the "save at revision" command to do this.

Steps:

  • Run Tortoise Workbench

  • Find the changeset in history which has the file revision you want (could be in any branch)

    • Say its changeset #abcd1234
  • Right click on the specific file in the file list for that changeset

  • Select "Save at revision..." and tell it where to put the file.

  • The file would typically be named filename@abcd1234.ext so that you don't mix it up with the current version.

Similar to the other answer, you can use the cat command but in a slightly different way:

hg cat --rev=11204 path/to/file

This will pick the file from revision #11204 which can be any branch.

You may want to add the --output switch lets you use whatever filename and/or location you want:

hg cat --output=C:/new/path/to/file/filename@11204.ext --rev=11204 path/to/file

Use the cat subcommand with the revision number of the commit you're looking for.

Here's an example. I create two branches with the contents of the file file indicating which branch it's on. Then I use cat to examine the contents on both branches

% hg init foo

% cd foo

% echo "default branch" > file

% hg add file

% hg ci -m 'default' file

% echo "default 2nd rev" > file

% hg ci -m 'default 2nd rev' file

% hg glog
@  changeset:   1:305b2b5ccbd9
|  tag:         tip
|  user:        dj
|  date:        Mon Sep 17 14:11:20 2018 -0400
|  summary:     default 2nd rev
|
o  changeset:   0:6d22c68d8abc
   user:        dj
   date:        Mon Sep 17 14:10:56 2018 -0400
   summary:     default

% hg co -r 0
1 files updated, 0 files merged, 0 files removed, 0 files unresolved

% echo "new branch" > file

% hg branch 'new branch'
marked working directory as branch new branch
(branches are permanent and global, did you want a bookmark?)

% hg ci -m 'new branch'

% hg glog
@  changeset:   2:4c8d2181526c
|  branch:      new branch
|  tag:         tip
|  parent:      0:6d22c68d8abc
|  user:        dj
|  date:        Mon Sep 17 14:12:30 2018 -0400
|  summary:     new branch
|
| o  changeset:   1:305b2b5ccbd9
|/   user:        dj
|    date:        Mon Sep 17 14:11:20 2018 -0400
|    summary:     default 2nd rev
|
o  changeset:   0:6d22c68d8abc
   user:        dj
   date:        Mon Sep 17 14:10:56 2018 -0400
   summary:     default

% hg branches
new branch                     2:4c8d2181526c
default                        1:305b2b5ccbd9

% hg cat -r 2 file
new branch

% hg cat -r 1 file
default 2nd rev
Related