Friday, July 5, 2024
HomeTutorialsHow to Overwrite Local Branch with Remote in Git

How to Overwrite Local Branch with Remote in Git

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.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.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.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.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.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.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.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

Nango Kalahttps://www.kala.co.za
Experienced Support Engineer with a demonstrated history of working in the information technology and services industry. Skilled in Microsoft Excel, Customer Service, Microsoft Word, Technical Support, and Microsoft Office. Strong information technology professional with a Microsoft Certificate Solutions Expert (Privet Cloud) focused in Information Technology from Broadband Collage Of Technology.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments