Skip to content

Instantly share code, notes, and snippets.

@fieldse
Last active October 18, 2023 09:32
Show Gist options
  • Save fieldse/b6784b17a540a255b12e594dbc9cfd4b to your computer and use it in GitHub Desktop.
Save fieldse/b6784b17a540a255b12e594dbc9cfd4b to your computer and use it in GitHub Desktop.
How to perform a hard reset of a git branch to master

Git - Hard reset branch to current master

Scenario

  • You have Git branches main and branch staging.
  • You need to reset staging exactly to the state of branch main.

Warnings

Be sure you really want to do this:

  1. This is a destructive operation, and will erase the commit history of the branch
  2. This will affect any other developers using the branch.

How to do it (the correct way)

git checkout main
git pull origin main
git checkout staging
git reset --hard main
git push --force origin staging

Result

  • Your branch staging is now exactly in the current state of branch main
  • The commit history of branch staging is now erased, and mirrors the commit history of main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment