Linux Comm Hg
[ Linux Command Manual](#)
* * *
hg is the command-line tool for the Mercurial distributed version control system. Mercurial is a lightweight distributed version control system written in Python, similar to Git but with a simpler design.
* * *
## hg Basic Concepts
### Version Control System
Version Control System (VCS) is used to track file change history and support multi-person collaborative development. It is divided into centralized (such as SVN) and distributed (such as Git, Mercurial).
### Distributed VS Centralized
* **Distributed**: Every developer has a complete copy of the repository and 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
## Examples
# Ubuntu/Debian
sudo apt-get install mercurial
# CentOS/RHEL
sudo yum install mercurial
# macOS (using Homebrew)
brew install mercurial
### Basic Configuration
Edit the `~/.hgrc` file:
## Examples
username= Your Name
* * *
## hg Common Commands Explained
### Repository Operations
## Examples
# Initialize new repository
hg init project-name
# Clone existing repository
hg clone https://example.com/repo
### File Tracking
## Examples
# Add file to version control
hg add filename
# Check repository status
hg status
# Commit changes
hg commit -m"commit message"
### Branch Management
## Examples
# Create new branch
hg branch branch-name
# Switch branch
hg update branch-name
# View all branches
hg branches
### Change History
## Examples
# View commit history
hg log
# View modification history of specific file
hg log filename
# Show changes
hg diff
### Remote Operations
## Examples
# Pull remote changes
hg pull
# Push local changes
hg push
# View remote repository
hg paths
* * *
## hg Advanced Usage
### Undoing Changes
## Examples
# Revert working directory modifications
hg revert filename
# Roll back to specific version
hg update -r version-number
### Merging Branches
## Examples
# Merge another branch into current branch
hg merge branch-name
# Mark as resolved after resolving conflicts
hg resolve -m filename
### Tagging
## Examples
# Create tag
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
## Examples
# 1. Create project directory
mkdir myproject
cd myproject
# 2. Initialize repository
hg init
# 3. Create file and add
echo"Hello World"> README.txt
hg add README.txt
# 4. Commit initial version
hg commit -m"Initial commit"
### Example 2: Team Collaboration Workflow
## Examples
# 1. Clone remote repository
hg clone https://team-server.com/project
# 2. Create feature branch
hg branch feature-x
hg commit -m"Start feature X"
# 3. Push to server after development
hg push --new-branch
# 4. Other members get updates
hg pull
hg update feature-x
* * *
## Common Problem Solutions
### Problem 1: Committed Wrong Content
## Examples
# Rollback to last commit
hg rollback
# Or create reverse change
hg backout wrong-commit-ID
### Problem 2: Merge Conflicts
1. Conflict files will contain markers
2. Edit file to resolve conflicts
3. Mark as resolved
4. Complete merge commit
## Examples
hg resolve -m conflict-file
hg commit -m"Merge conflict resolved"
### Problem 3: Recover Deleted Files
## Examples
# View deletion history
hg log --removed
# Restore file from specific version
hg revert -r version-number filename
* * *
## Best Practice Recommendations
1. **Frequent commits**: Commit in small steps, keep each commit atomic
2. **Descriptive messages**: Commit messages should clearly explain the changes
3. **Branch strategy**: Use different strategies for long-term branches (such as development, release) and short-term feature branches
4. **Regular synchronization**: Frequently execute pull/push to stay in sync with the team
5. **Backup important branches**: Push critical branches to remote server
* * *
## Learning Resources Recommendations
1. Official documentation: [https://www.mercurial-scm.org/guide](https://www.mercurial-scm.org/guide)
2. "Mercurial: The Definitive Guide" e-book
3. hg built-in help: `hg help` or `hg help command-name`
4. Interactive tutorial: [https://hginit.com/](https://hginit.com/)
By mastering hg commands, you can efficiently manage project versions and collaborate with your team. Although Git is more popular currently, Mercurial's simple design makes it an excellent choice in certain scenarios.
* * Linux Command Manual](#)
YouTip