Reset Local Master Branch to Match Remote

GitGitBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

When working with Git, it is common to have a local branch that is out of sync with its remote counterpart. This can happen when changes are made to the remote branch that are not reflected in the local branch. In such cases, it is necessary to reset the local branch to match the remote branch. This lab will guide you through the steps to reset the local master branch to match the one on the remote.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL git(("`Git`")) -.-> git/DataManagementGroup(["`Data Management`"]) git/DataManagementGroup -.-> git/reset("`Undo Changes`") subgraph Lab Skills git/reset -.-> lab-12753{{"`Reset Local Master Branch to Match Remote`"}} end

Reset Local Master Branch to Match Remote

You have been working on a project and have made changes to the local master branch. However, you realize that the remote master branch has been updated with new changes that you do not have in your local branch. You need to reset the local master branch to match the one on the remote.

  1. Switch to the master branch:
    git checkout master
  2. Retrieve the latest updates from the remote:
    git fetch origin
  3. View the commit history of the current branch:
    git log
  4. Reset the local master branch to match the one on the remote:
    git reset --hard origin/master
  5. Verify that the local master branch is now up to date with the remote master branch:
    git log

This is the finished result:

commit d22f46ba8c2d4e07d773c5126e9c803933eb5898 (HEAD -> master, origin/master, origin/feature-branch, origin/HEAD)
Author: Hang <[email protected]>
Date:   Wed Apr 26 14:16:25 2023 +0800

    Added file2.txt

commit cf80005e40a3c661eb212fcea5fad06f8283f08f
Author: Hang <[email protected]>
Date:   Wed Apr 26 14:16:25 2023 +0800

    Added file1.txt

commit b00b9374a7c549d1af111aa777fdcc868d8a2a01
Author: Hang <[email protected]>
Date:   Wed Apr 26 14:16:00 2023 +0800

    Initial commit

Summary

Resetting the local master branch to match the one on the remote is a common task when working with Git. By following the steps outlined in this challenge, you can ensure that your local branch is up to date with the remote branch. Remember to use git fetch origin to retrieve the latest updates from the remote, git checkout master to switch to the master branch, and git reset --hard origin/master to reset the local master branch to match the one on the remote.

Other Git Tutorials you may like