How to Overwrite Local Branch with Remote in Git

August 31, 2023

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.

How to overwrite a local Git branch with a remote one - a tutorial.

Prerequisites

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:

Switching branches in Git.

2. Fetch the remote changes to get the latest version of the remote branch. Run the following command:

git fetch
Fetching changes from the remote repository.

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:

Using hard reset to overwrite a local branch in Git.

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:

Deleting a branch in Git.

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:

Fetching a remote branch to a remote repository.

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:

Rebuilding a branch from a remote one in Git.

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.

Was this article helpful?
YesNo
Bosko Marijan
Having worked as an educator and content writer, combined with his lifelong passion for all things high-tech, Bosko strives to simplify intricate concepts and make them user-friendly. That has led him to technical writing at PhoenixNAP, where he continues his mission of spreading knowledge.
Next you should read
How To Rename a Local and Remote Git Branch
April 11, 2024

Git uses named branches to keep the original code intact. This guide uses a few simple commands to show you how to change the name of a branch.
Read more
Git: Clone a Specific Branch
April 6, 2023

The git clone command is usually used to clone an entire repository to a local machine. This tutorial shows how to use the command to clone only a specific Git branch.
Read more
Git Merge Master into Branch
March 22, 2023

This tutorial shows two methods for merging the master branch into another branch in Git. See examples and use cases.
Read more
How to Pull All Branches in Git
March 16, 2023

This step-by-step tutorial shows how to pull all branches from a remote repository into a local one in Git. Choose from two different methods and start working with Git branches.
Read more