Introduction
A Git branch allows users to isolate and manage different lines of development within a project without affecting the main codebase until the changes are merged. A local branch exists on your local machine, while a remote Git branch exists on a remote repository like GitHub. The remote branch provides access to other team members for collaboration.
Overwriting a local Git branch with a remote one can be helpful in several scenarios. For example, synchronization of your local branch with the changes that have been made on the remote repository.
In this tutorial, you will learn to overwrite a local Git branch with a remote one.
Prerequisites
- Git installed (install Git on Ubuntu, macOS, Windows, CentOS 7, or CentOS 8).
- A remote and local Git repository.
Overwrite Local Branch via git reset Command (Hard Reset)
git reset
is a Git command that moves the current branch's HEAD
to the specified commit, resetting the branch to a different point in its history. It discards all changes in the staging area and the working directory, which reverts the project to the state of the specified commit.
The syntax for the hard reset is:
git reset --hard [commit]
Important: Use hard reset cautiously, as it discards any uncommitted changes permanently.
Follow the steps below to overwrite the local branch using git reset
:
1. Use git checkout
to switch to the branch you want to overwrite. The syntax is:
git checkout [branch-name]
For example:
2. Fetch the remote changes to get the latest version of the remote branch. Run the following command:
git fetch
3. Run the following command to overwrite the local branch with the remote one:
git reset --hard @{u}
@{u}
is a shorthand for the upstream branch that your current branch is tracking. The shorthand is useful if you are not sure of the name of the remote branch, as you don't have to specify it.
For example:
The command resets the current HEAD
to the same commit as the one in the upstream branch.
However, if you want to specify the branch name, use the following syntax:
git reset --hard [upstream_branch_name]
Replace [upstream_branch_name]
with the remote branch name.
Note: Need help determining which branches exist remotely? See how to list remote branches in Git.
Overwrite Local Branch by Rebuilding the Local Branch
Rebuilding a branch in Git refers to recreating a branch with the same name but with potentially different content. Rebuilding is another way to overwrite a local branch with a remote one. This process allows users to correct mistakes, integrate changes from other branches, or reset a branch to a specific state.
Follow the steps below to overwrite a branch using rebuild:
1. Back up your local branch before making significant changes if you are working with sensitive or critical data. Use the following syntax:
git checkout -b [backup/feature-branch] [feature-branch]
The syntax above creates a new branch that acts as a backup of your existing branch. This method creates an exact copy of the branch's state, which you can revert to if needed.
2. Delete the local branch you want to rebuild. The syntax is:
git branch -D [local_branch]
For example:
Here, we deleted the new-branch branch.
3. Fetch the latest copy of the remote branch from the remote repository. The syntax is:
git fetch [remote_repo] [remote_branch]
For instance:
In the example above, we fetch the new-branch branch from the origin repository.
4. Rebuild the local branch based on the remote branch you have just fetched. Use the git checkout
command to create a new branch based on the fetched one and switch to that branch. The syntax is:
git checkout -b [local_branch] [remote_branch]
For example:
In the example above, we rebuild the new-branch branch based on the upstream branch fetched in the steps above.
Note: When you finish making changes, share them with your team members by pushing the changes to the remote branch.
Conclusion
This tutorial has shown two methods for overwriting a local Git branch with a remote one. Overwriting a branch with a remote one is useful when you have an older version of the branch locally and you want to avoid copying the entire repository or when you need to synchronize with the remote changes.
For more Git tutorials, see how to compare two Git branches or learn to checkout a file from another branch.