如何在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

