← homeProgramming (Програмування)

How to quickly reset the QA/Staging branch to the state of main

Sometimes the QA (or staging) branch becomes so cluttered with experiments that it's easier not to try to resolve all conflicts and just make it an exact copy of main (master).For this, two commands are enough:git res...

This content has been automatically translated from Ukrainian.
Sometimes the QA (or staging) branch becomes so cluttered with experiments that it's easier not to try to resolve all conflicts and just make it an exact copy of main (master).
For this, two commands are enough:
git reset --hard origin/main
git push --force-with-lease
Example:
git reset --hard origin/main
HEAD is now at 46911f2 Fixed main page search

git push --force-with-lease
Total 0 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0)
To https://github.com/username/sandbox.git
 + 53cda29...46911f2 staging -> staging (forced update)
What happens:
  • git reset --hard origin/main completely resets the current local branch to the same commit as origin/main. All local commits and uncommitted changes will be lost.
  • git push --force-with-lease updates the remote QA/Staging branch, making it an exact copy of main.
Why --force-with-lease instead of --force?
Because this command is a bit safer. Git checks that no one has pushed new commits after your last fetch. If the remote branch has changed, the push will be rejected instead of silently overwriting someone else's work.
⚠️ Do this only when you really want to completely overwrite the history of the QA/Staging branch. After such a push, all commits not present in main will cease to be part of this branch.
GUI applications like Github Desktop may not reflect these changes. You may need to restart the application or "jump" between branches to pull in the changes.

🔥 More posts

All posts
What is ORM and why is it needed?
Programming (Програмування)Oct 26, '25 14:00

What is ORM and why is it needed?

When we work with databases, we usually have to write SQL queries - selections, inserts, updates,...