How to reset a Git branch to a remote repository

A Git branch can be reset to exactly match the remote branch with the following commands:

  1. Save the state of your current branch in another branch, named my-backup,in case something goes wrong:

    git commit -a -m "Backup."
    git branch my-backup
    
  2. Fetch the remote branch and set your branch to match it:

    git fetch origin
    git reset --hard origin/master
    

This example assumes that the remote repo’s name is “origin” and that the branch named “master”, ​in the remote repo, matches the currently checked-out branch in your local repo.

Copyright ©2024 Educative, Inc. All rights reserved