Linux hg command
hg is the command-line tool of 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
A version control system (VCS) is used to track the history of file changes and supports collaborative development by multiple people. 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 its history
- Centralized: Only one central repository stores all the history
Mercurial features
- Simple and intuitive command design
- High performance for handling large projects
- Cross-platform support
- Extensible plugin system
hg installation and configuration
Installation Method
Example
Ubuntu/Debian
sudo
apt-get install
mercurial
CentOS/RHEL
sudo
yum install
mercurial
macOS (using Homebrew)
brew
install
mercurial
Basic configuration
Editing ~/.hgrc File:
Example
[ui]
username
=
Your Name
hg common commands in detail
Repository operations
Example
Initialize a new repository
hg init project name
Clone an existing repository
hg clone https:
//
example.com
/
repo
File tracking
Example
Add files to version control
hg add file name
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
Change history
Example
View commit history
hg log
View the modification history of a specific file
hg log file name
Display change content
hg
diff
Remote operations
Example
Pull remote changes
hg pull
Push local changes
hg push
View the remote repository
hg paths
hg advanced usage
Undo changes
Example
Undo working directory modifications
hg revert file name
Roll back to a specific version
hg update
-r
Version number
Merge branches
Example
Merge another branch into the current branch
hg merge branch name
Mark as resolved after resolving conflicts
hg resolve
-m
File Name
Tagging
Example
Create a tag
hg tag tag name
View all tags
hg tags
hg compared with Git
| 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: Create a new project
Example
1. Create the project directory
mkdir
myproject
cd
myproject
2. Initialize the repository
hg init
3. Create files and add them
echo
"Hello World"
&
gt; 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. Push to the server after development is complete
hg push
--new-branch
4. Other members pull updates
hg pull
hg update feature-x
Common Problem Solutions
Problem 1: Committed the wrong content
Example
Roll back to the last commit
hg rollback
Or create a reverse change
hg backout wrong commit ID
Problem 2: Merge conflict
- Conflict files will contain markers
- Edit the file to resolve the conflict
- Mark as resolved
- Complete the merge commit
Example
hg resolve
-m
Conflict file
hg commit
-m
"Merge conflict resolved"
Problem 3: Recover a deleted file
Example
View the deletion history
hg log
--removed
Restore a file from a specific version
hg revert
-r
version number file name
Best Practice Recommendations
- Commit frequently: Commit in small steps, keeping each commit atomic
- Descriptive messages: Commit messages should clearly describe the changes
- Branching strategy: Use different strategies for long-lived branches (such as development and release) and short-lived feature branches
- Regular synchronization: Frequently perform pull/push to stay in sync with the team
- Back up important branches: Push key 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.
Linux Command Reference