Skip to main content

Discard all local changes and pull the latest version from a GitHub

· 5 min read
Serhii Hrekov
software engineer, creator, artist, programmer, projects founder

To discard all your local changes and pull the latest version from a GitHub repository, you need to completely reset your local branch to match the remote's state. This process involves a few steps to ensure all uncommitted and committed local changes are removed.

This guide covers the most reliable methods, including situations where you've already made local commits.

Discarding Uncommitted Changes

If you have local modifications that you haven't committed yet, you can use git restore or git checkout to discard them.

# Discard all changes in the working directory
git restore .

# (Older command) Discard all changes in the working directory
git checkout -- .

After running this, your working directory will be clean, and all your local modifications will be gone.


Discarding Committed Changes

If you have already made one or more local commits that you want to discard, you need to use git reset. The --hard flag is crucial here, as it tells Git to reset your branch pointer and also wipe out the changes from your working directory and staging area.

# This command forcefully moves your branch pointer to match the remote's
# and discards all local changes and commits.
git reset --hard origin/main

The origin/main part of the command is key. It tells Git to reset your local main branch to the exact state of the main branch on the origin remote. If your branch is named differently (e.g., dev), replace main with the correct branch name.

What happens if you forget to fetch?

A common mistake is running git reset --hard without a recent git fetch. This would only reset your branch to the last time you fetched, not the absolute latest version on GitHub.

To be completely safe, you should always run git fetch before resetting.

# 1. Fetch the latest changes from the remote
git fetch origin

# 2. Forcefully reset your local branch to match the remote's state
git reset --hard origin/main

This two-step process guarantees that your local branch will be a perfect, clean copy of the remote branch.


Some developers might try git pull --force or similar commands, but this is not the right tool for this job (1). The purpose of git pull is to integrate changes, not to discard them. Forcing a pull can lead to unexpected merge conflicts or an unmanageable commit history if you have made local changes that are not discarded correctly.

The combination of git fetch and git reset --hard is the safest, most explicit, and most reliable method for discarding all local changes and synchronizing with the remote repository. It leaves no room for ambiguity about your intent to overwrite your local work.

Summary of Commands

CommandPurposeWhen to Use
git fetch originDownloads the latest remote state without merging.Always run this first for a full reset.
git reset --hard origin/mainForcefully discards local commits and changes.The core command to completely wipe your local branch.
git restore .Discards uncommitted changes in your working directory.When you just want to revert uncommitted files.

By using git fetch followed by git reset --hard, you are guaranteed to be in sync with the remote repository, no matter what state your local branch was in.

Sources

  1. Git Documentation: git pull