Git

Git Replace Local Version With Remote Version

Git branches are the basic fundamentals of a Git repository where developers can implement and test new features. Sometimes, they want to ignore their local branch content and update it with the remote branch without merging and causing conflicts. So, to avoid problems, it is required to replace the local branch with a remote one.

This blog provides the easiest procedure to replace the local version with the remote version on Git.

How to Replace Local Version With Remote Version in Git?

To replace the local version of the branch with the remote version, first, go to the required repository. Then, view all the available branches in your local repository and switch to your desired branch. Next, fetch the remote branches. Lastly, run the “git reset –hard <remote-name>/<branch-name>” command to get the latest version of the remote branch.

To do so, follow the below-presented steps.

Step 1: Go to Git Local Git Repository
Utilize the “cd” command and navigate to the particular Git repository:

$ cd "C:\Git\test_repo"

Step 2: View All the Local Branches
Next, view the list of all branches by using the following command:

$ git branch -a

In the below output, the asterisk “*” sign beside the “dev” branch represents the current working branch:

Step 3: Switch Branch
Now, switch to the desired local branch to which you want to replace it with the remote branch version:

$ git switch master

As you can see, we have switched to the specified “master” branch:

Step 4: Check Remote URLs
To view the list of all available remote URLs, execute the below-stated command:

$ git remote -v

Step 5: Fetch the Remote Branch
Then, download the updated version of the remote repository by fetching its content through the “git fetch” command:

$ git fetch origin

Step 6: Replace Local Version With Remote Version
Finally, run the “git reset” command along with the “–hard” option and required remote branch name:

$ git reset --hard origin/master

Here, you can see the “HEAD” pointer moved its position to “dd9c220” commit SHA hash:

Step 7: Check Git Log
Check the Git logs to verify whether the local branch is replaced exact as a remote branch or not:

$ git log

The output below shows that the local branch version is replaced with a remote branch version successfully:

We have efficiently explained the method of replacing the local version with the remote version.

Conclusion

To update the local version with the latest remote version, first, navigate to the local repository. Then, view the list of all existing branches and switch to the targeted branch which needs to be replaced with the remote branch. After that, fetch the remote repository content and run the “git reset –hard <remote-name>/<branch-name>” command to replace the local branch with the remote branch. Lastly, check the Git log to see the changes. This blog explained the method of replacing the local version with the remote version.

About the author

Sharqa Hameed

I am a Linux enthusiast, I love to read Every Linux blog on the internet. I hold masters degree in computer science and am passionate about learning and teaching.