Introduction
Branching in Git is a fundamental concept that allows developers to work on different aspects of a project simultaneously without affecting the main codebase. Each branch represents a distinct line of development and facilitates experimentation and feature isolation.
Resetting a local Git branch to a remote one synchronizes a local branch with the changes made in a remote repository. This process ensures your local work remains aligned with the collaborative development efforts stored in the remote repository.
In this tutorial, you will learn to reset a local branch to a remote one.
Prerequisites
- Git installed (see how to install Git on Ubuntu, macOS, Windows, CentOS 7, or CentOS 8).
- A remote and local Git repository.
Reset a Local Branch to Remote Branch
This section shows how to reset a local branch to a branch in the remote repository. Depending on what you want to achieve, Git allows you to:
- Reset the local branch to the
HEAD
of the remote branch it is tracking. - Reset the local branch to any other branch from the remote repository.
Refer to the sections below for a step-by-step guide to each solution.
Reset a Local Branch to Any Remote Branch
To reset a local branch to match any remote branch in Git, use the git reset
and git fetch
commands. Follow the steps below:
1. Ensure you are on the local branch you want to reset. Switch to the branch using the git checkout
command. The syntax is:
git checkout [branch_name]
For example:
The command switches to the specified branch.
2. Fetch the latest changes from the remote repository. This step ensures you get the latest updates stored on the remote repository. Use the following syntax:
git fetch [remote_name]
Replace [remote_name]
with the name of your remote repository. For example:
git fetch origin
3. Create a backup of your local branch before resetting it in case something goes wrong. You can clone the branch or use the git branch
command. The syntax is:
git branch [branch-backup]
Replace [branch-backup]
with a name for the branch copy. The command creates a new branch with the specified name, and it is a copy of the branch you were on when you issued the command.
4. Reset the local branch to match the remote branch you want using the git reset
command. Use the following syntax:
git reset --hard [remote_name]/[branch_name]
Replace [remote_name]
with the name of your remote repository and [branch_name]
with the branch you want to reset to.
For example:
Important: The --hard
option forcefully resets your local branch to match the specified one, discarding any local changes that haven't been committed. Use the --hard
option only when you want to discard any uncommitted local changes. Use the --soft
option instead of --hard
to preserve local changes.
5. Optionally, clean up the working tree after resetting if some files and directories remain. Use the following command to clean up the working tree by recursively removing files from the earlier branch that are not under version control:
git clean -xdf
-x
- removes all untracked files, including ignored build directories.-d
- allows Git to recurse into untracked directories when no path is specified.-f
- overwrites the default Git clean configuration and removes untracked files and directories.
Reset a Local Branch to a Remote Head
In Git, a remote HEAD
is the tip of a branch in a remote repository. Remote heads represent the latest commit on a specific branch in the remote repository. It is a way for the local Git repository to track and stay updated with changes made in the remote repository.
Resetting a local branch to a remote HEAD
is useful if you want to undo unwanted changes, unstage changes, fix a bad commit, or split a commit.
Follow the steps below to reset a local branch to a remote HEAD
:
1. Switch to the branch you want to reset using git checkout
. The syntax is:
git checkout [branch_name]
2. Fetch the remote changes to update the information about the remote heads. Run the following command:
git fetch
3. Run the following command to reset the local branch with the remote one:
git reset --hard HEAD
The command resets the current HEAD
to the same one as in the remote branch.
Reset a Local Branch to an Upstream Branch
An upstream branch is a branch in the remote repository that your local branch tracks for synchronization purposes. The upstream branch defines where the local branch should push to and pull from by default.
Use the git reset
command with the upstream branch reference to reset a local branch to match its upstream branch. Follow the steps below:
1. Switch to the local branch you want to reset. Use the following syntax:
git checkout [branch_name]
2. Reset the local branch to match its upstream branch using the git reset
command. Run the following command:
git reset --hard @{u}
The @{u}
notation is a shorthand for the upstream branch. It allows you to reset the local branch to the upstream one without specifying the upstream branch name. The feature is useful if you don't know the upstream branch name.
It is also possible to explicitly specify the upstream branch name instead of the @{u}
notation. The syntax is:
git reset --hard [upstream_branch_name]
The command synchronizes the local branch with its upstream branch.
Conclusion
This tutorial showed how to reset a local Git branch to a remote one in several ways - to a remote head, to any remote branch, or to an upstream branch it is tracking.
For more Git tutorials, see how to compare two Git branches.