You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Mercurial中获取指定分支文件的最早5个版本

获取Mercurial指定分支某文件最早5个版本的方法

Got it, let's skip the tedious manual scrolling—here's a straightforward way to grab the earliest 5 versions of your target file on a specific branch, all from the command line in a headless Linux environment:

Step 1: List all revisions of the file (sorted oldest to newest)

First, we need to get the full history of the file on your target branch, ordered from the earliest commit to the latest. Mercurial's hg log defaults to newest-first, so we'll reverse it with --reverse:

hg log -b YOUR_BRANCH_NAME -f PATH_TO_YOUR_FILE --reverse
  • -b YOUR_BRANCH_NAME: Restricts the log to only revisions on your specified branch
  • -f: Follows the file through any renames/moves (critical if the file was ever renamed)
  • --reverse: Flips the order to show oldest revisions first

Step 2: Extract the first 5 revision IDs

If you just want the raw revision hashes (or numbers) for the 5 earliest versions, use the --template flag to output only the revision IDs, then pipe to head to grab the first 5:

hg log -b YOUR_BRANCH_NAME -f PATH_TO_YOUR_FILE --reverse --template "{node}\n" | head -n 5
  • {node}: Outputs the full 40-character revision hash (more reliable than revision numbers, which can change across clones)
  • Replace {node} with {rev} if you prefer the shorter numerical revision ID instead

Step 3: Export each of the 5 versions

To save each version as a separate file (so you can review them), use a simple loop to run hg cat for each revision:

for rev in $(hg log -b YOUR_BRANCH_NAME -f PATH_TO_YOUR_FILE --reverse --template "{node}\n" | head -n 5); do
  hg cat -r $rev PATH_TO_YOUR_FILE > "YOUR_FILE_NAME_$rev.txt"
done

This will create files like my_script.py_abc123def456...txt, each containing the file's content at that earliest revision.

Pro Tips

  • If you want to verify the revisions before exporting, adjust the template to include commit messages: "{node}: {desc}\n"
  • If your branch name has spaces, wrap it in quotes: -b "my feature branch"
  • If you don't need to save the files and just want to view them in order, pipe the revision IDs to xargs hg cat:
    hg log -b YOUR_BRANCH_NAME -f PATH_TO_YOUR_FILE --reverse --template "{node}\n" | head -n 5 | xargs -I {} hg cat -r {} PATH_TO_YOUR_FILE
    

内容的提问来源于stack exchange,提问作者JCF

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 07:56:50