Git equivalent to hg update
Categories:
Git Equivalent to 'hg update': Navigating Your Repository History

Understand how Git's branching and checkout mechanisms provide the same functionality as Mercurial's 'hg update' command, allowing you to switch between different states of your project.
Developers migrating from Mercurial (Hg) to Git often look for direct command equivalents. One common point of confusion is how to replicate the behavior of hg update
. In Mercurial, hg update
is used to switch the working directory to a specific revision, branch, or tag. Git achieves this same goal, but through a slightly different philosophy and set of commands, primarily git checkout
and git switch
.
Understanding 'hg update' in Mercurial
In Mercurial, hg update
is a versatile command. Without arguments, it updates your working directory to the tip of the current branch. With an argument, such as a revision number, branch name, or tag, it switches your working directory to that specific point in history. This command effectively changes the state of your files to match the selected revision, making it central to navigating your project's history and working on different features or bug fixes.
# Update to the tip of the current branch
hg update
# Update to a specific branch
hg update feature-branch
# Update to a specific revision
hg update 12345:abcdefg
# Update to a specific tag
hg update v1.0
Common uses of 'hg update' in Mercurial
The Git Equivalents: git checkout
and git switch
Git doesn't have a single command that directly maps to hg update
. Instead, its functionality is primarily covered by git checkout
and the newer, more focused git switch
command. Both commands allow you to move your HEAD
pointer to a different branch, commit, or tag, thereby updating your working directory to reflect that state.
flowchart TD A[Start] A --> B{Want to change working directory state?} B -->|Yes| C[Identify Target: Branch, Commit, or Tag] C --> D{Is target a branch?} D -->|Yes| E[Use `git switch <branch-name>`] D -->|No| F[Use `git checkout <commit-hash-or-tag>`] E --> G[Working directory updated to branch tip] F --> H[Working directory updated to specific commit/tag (detached HEAD)] G --> I[Continue work] H --> J{Want to create new branch from here?} J -->|Yes| K[Use `git switch -c <new-branch-name>`] J -->|No| L[Continue work or return to branch] K --> I L --> I
Decision flow for navigating Git history
Using git checkout
Historically, git checkout
was the primary command for switching branches, restoring files, and creating new branches. While still widely used, its overloaded nature led to the introduction of git switch
for clarity in branch operations. However, git checkout
remains essential for checking out specific commits or tags, which often results in a 'detached HEAD' state.
# Switch to an existing branch (equivalent to 'hg update feature-branch')
git checkout feature-branch
# Switch to a specific commit (detached HEAD state)
git checkout abcdefg
# Switch to a specific tag (detached HEAD state)
git checkout v1.0
# Create and switch to a new branch
git checkout -b new-feature-branch
Examples of 'git checkout' for history navigation
Using git switch
(Recommended for Branch Operations)
Introduced in Git 2.23, git switch
provides a clearer and safer way to switch branches. It separates the concerns of switching branches from restoring files, making the command-line interface more intuitive. For simply moving between branches, git switch
is the preferred command.
# Switch to an existing branch (preferred over 'git checkout <branch>')
git switch feature-branch
# Create and switch to a new branch
git switch -c new-feature-branch
# Create a new branch from a specific commit/tag and switch to it
git switch -c hotfix-branch abcdefg
Examples of 'git switch' for branch management
git checkout
a specific commit or tag (not a branch), you enter a 'detached HEAD' state. This means your HEAD
is pointing directly to a commit, not a branch. Any new commits you make will not be part of any branch unless you explicitly create a new branch from that point. Use git switch -c <new-branch-name>
to create a branch and attach your HEAD to it.Summary of Equivalents
To summarize the hg update
functionality in Git:

Mercurial hg update
vs. Git git checkout
/git switch
1. To switch to an existing branch
Use git switch <branch-name>
(recommended) or git checkout <branch-name>
. This updates your working directory to the latest state of that branch.
2. To switch to a specific commit or tag
Use git checkout <commit-hash-or-tag>
. Be aware that this puts you in a 'detached HEAD' state. If you want to make changes, create a new branch immediately with git switch -c <new-branch-name>
.
3. To create and switch to a new branch
Use git switch -c <new-branch-name>
(recommended) or git checkout -b <new-branch-name>
. This creates a new branch pointing to your current HEAD
and then switches to it.