Data report"State of code review 2024" is now liveRead the full report

Git hard reset to remote

Greg Foster
Greg Foster
Graphite software engineer


Note

This guide explains this concept in vanilla Git. For Graphite documentation, see our CLI docs.


Hard resetting in Git is a powerful operation that can synchronize your local branch with a remote branch, discarding all local changes and making your branch identical to the remote one. This guide will explain how to perform a hard reset to a remote branch in Git, including various scenarios and keywords related to this operation.

Stop wrestling with Git commands

The Graphite CLI takes all the pain out of Git, allowing you to ship faster and stop Googling Git commands.

Learn more

Before diving into the hard reset, let's define some key Git terms:

  • Branch: A parallel version of a repository. It serves as an independent line of development.
  • Remote: A common repository on a server that acts as a central point of truth between collaborators.
  • Commit: A snapshot of your work at a particular point in time.

A hard reset should be used with caution as it can lead to loss of local changes. It is commonly used in the following scenarios:

  • Synchronizing a local branch with the latest changes in the remote repository, especially if a rebase or merge is not feasible.
  • Discarding local changes that are no longer needed or are incorrect.
  • Reverting a branch to a clean state before applying new changes.

A hard reset changes the HEAD, the pointer to the tip of a branch, to a specific state

To perform a hard reset to a remote branch, you'll typically follow these steps in your terminal:

  1. Fetch the latest changes from the remote repository:

    Terminal
    git fetch origin

    This command updates your remote-tracking branches, the pointers that connect a local copy of a branch to its remote counterpoint, under refs/remotes/origin/.

    git fetch will fetch all of the changes that you do not have yet from the remote repository without merging those changes into your local branch.

  2. Check the status of your branch:

    Terminal
    git status

    This will help you see if there are any changes that will be discarded by a hard reset. If you see any changes under the header Changes not staged for commit: these changes will be discarded upon a hard reset.

  3. Hard reset your local branch to match the remote branch:

    Terminal
    git reset --hard origin/main

    Replace main with the appropriate branch if you're working with a different branch. This command resets the index and working tree. Any changes to tracked files in the working tree since origin/main are discarded. If you want to keep any of these local changes, you can either stash them, which moves them from your local directory to a separate local cache called the "stash", or commit and push these changes to the remote repository.

    If you choose to stash these changes you can then move them back to the local working directory with the command git stash pop, this pops the most recent stash off of the stash stack and merges them with your local state.

The best engineers use Graphite to simplify Git

Engineers at Vercel, Snowflake & The Browser Company are shipping faster and staying unblocked with Graphite.

Git started for free
  • Reset and push to a specific commit: If you need to reset your branch to a specific commit on the remote and push that change:

    Terminal
    git reset --hard <commit-hash>
    git push origin main --force

    This will overwrite any commits made since the specified commit and will restore main to the state captured at the time of that commit.

  • Discard local changes after a bad merge: If a merge went wrong and you want to start over by matching the remote:

    Terminal
    git reset --hard origin/main

    This will restore your local state to the latest state of the main branch.

While git reset --hard is powerful, it's also potentially destructive. Here are some best practices to follow:

  • Always make sure you do not need your local changes before performing a hard reset.
  • Consider using other less destructive commands like git stash or git revert if you need to preserve history or manage changes more carefully.
  • Communicate with your team when performing force pushes, especially in shared branches.

For further reading on hard resets, see the official Git documentation.

Stay unblocked. Ship faster.
Experience the new developer workflow - create, review, and merge code continuously. Get started with one command.
Learn more

Give your PR workflow
an upgrade today

Stack easier | Ship smaller | Review quicker

Or install our CLI.
Product Screenshot 1
Product Screenshot 2