Linux hg Command
Complete Linux Command Reference
hg is the command-line tool for the Mercurial distributed version control system. Mercurial is a lightweight distributed version control system written in Python. It is similar to Git but with a simpler design.
hg Basic Concepts
Version Control System
A version control system (VCS) is used to track the history of file changes and support collaborative development by multiple people. It is divided into centralized systems (e.g., SVN) and distributed systems (e.g., Git, Mercurial).
Distributed vs Centralized
- Distributed: Every developer has a complete copy of the repository and its history.
- Centralized: Only one central repository stores all history.
Mercurial Features
- Simple and intuitive command design
- High performance handling large projects
- Cross-platform support
- Extensible plugin system
hg Installation and Configuration
Installation Methods
Example
# Ubuntu/Debian
sudo apt-get install mercurial
# CentOS/RHEL
sudo yum install mercurial
# macOS (using Homebrew)
brew install mercurial
sudo apt-get install mercurial
# CentOS/RHEL
sudo yum install mercurial
# macOS (using Homebrew)
brew install mercurial
Basic Configuration
Edit~/.hgrcfile:
Example
[ui]
username = Your Name
username = Your Name
hg Common Commands Explained
Repository Operations
Example
# Initialize a new repository
hg init project_name
# Clone an existing repository
hg clone https://example.com/repo
hg init project_name
# Clone an existing repository
hg clone https://example.com/repo
File Tracking
Example
# Add a file to version control
hg add filename
# View repository status
hg status
# Commit changes
hg commit -m "commit message"
hg add filename
# View repository status
hg status
# Commit changes
hg commit -m "commit message"
Branch Management
Example
# Create a new branch
hg branch branch_name
# Switch branches
hg update branch_name
# View all branches
hg branches
hg branch branch_name
# Switch branches
hg update branch_name
# View all branches
hg branches
Change History
Example
# View commit history
hg log
# View modification history of a specific file
hg log filename
# Show change content
hg diff
hg log
# View modification history of a specific file
hg log filename
# Show change content
hg diff
Remote Operations
Example
# Pull remote changes
hg pull
# Push local changes
hg push
# View remote repositories
hg paths
hg pull
# Push local changes
hg push
# View remote repositories
hg paths
hg Advanced Usage
Undoing Changes
Example
# Revert working directory modifications
hg revert filename
# Roll back to a specific version
hg update -rrevision_number
hg revert filename
# Roll back to a specific version
hg update -rrevision_number
Merging Branches
Example
# Merge another branch into the current branch
hg merge branch_name
# After resolving conflicts, mark as resolved
hg resolve -mfilename
hg merge branch_name
# After resolving conflicts, mark as resolved
hg resolve -mfilename
Tagging
Example
# Create a tag
hg tag tag_name
# View all tags
hg tags
hg tag tag_name
# View all tags
hg tags
hg vs Git Comparison
| Feature | hg (Mercurial) | Git |
|---|---|---|
| Learning curve | Gentler | Steeper |
| Command design | More consistent | More flexible |
| Branch model | Named branches | Lightweight branches |
| Windows support | Good native support | Requires Git Bash |
| Plugin system | Powerful | Powerful |
Practical Application Examples
Example 1: Creating a New Project
Example
# 1. Create a project directory
mkdir myproject
cd myproject
# 2. Initialize the repository
hg init
# 3. Create files and add them
echo "Hello World" > README.txt
hg add README.txt
# 4. Commit the initial version
hg commit -m "Initial commit"
mkdir myproject
cd myproject
# 2. Initialize the repository
hg init
# 3. Create files and add them
echo "Hello World" > README.txt
hg add README.txt
# 4. Commit the initial version
hg commit -m "Initial commit"
Example 2: Team Collaboration Workflow
Example
# 1. Clone the remote repository
hg clone https://team-server.com/project
# 2. Create a feature branch
hg branch feature-x
hg commit -m "Start feature X"
# 3. After development, push to the server
hg push --new-branch
# 4. Other members fetch updates
hg pull
hg update feature-x
hg clone https://team-server.com/project
# 2. Create a feature branch
hg branch feature-x
hg commit -m "Start feature X"
# 3. After development, push to the server
hg push --new-branch
# 4. Other members fetch updates
hg pull
hg update feature-x
Common Problem Solutions
Problem 1: Committed Incorrect Content
Example
# Roll back to the last commit
hg rollback
# Or create a reverse change
hg backout wrong_commit_id
hg rollback
# Or create a reverse change
hg backout wrong_commit_id
Problem 2: Merge Conflicts
- Conflict files will contain markers
- Edit the file to resolve conflicts
- Mark as resolved
- Complete the merge commit
Example
hg resolve -mConflict file
hg commit -m "Merge conflict resolved"
hg commit -m "Merge conflict resolved"
Problem 3: Recovering Deleted Files
Example
# View deletion history
hg log --removed
# Restore a file from a specific version
hg revert -rrevision_number filename
hg log --removed
# Restore a file from a specific version
hg revert -rrevision_number filename
Best Practice Suggestions
- Commit frequently: Commit in small steps, keep each commit atomic
- Descriptive messages: Commit messages should clearly describe the changes
- Branch strategy: Use different strategies for long-lived branches (e.g., development, release) and short-lived feature branches
- Regular synchronization: Frequently run pull/push to stay in sync with the team
- Back up important branches: Push critical branches to the remote server
Recommended Learning Resources
- Official documentation:https://www.mercurial-scm.org/guide
- "Mercurial: The Definitive Guide" eBook
- hg built-in help:
hg helporhg help 命令名 - Interactive tutorial:https://hginit.com/
By mastering hg commands, you can efficiently manage project versions and collaborate with your team. Although Git is currently more popular, Mercurial's simple design makes it an excellent choice in certain scenarios.
Other Extensions