Question

I have a Git/Gitlab repository. We used to commit straight to master, but we decided to switch to using feature branches like the rest of the world for this release.

We need to reset our remote master to the state it was in immediately after the last release. If someone has already committed to the master directly, how can I reset it to a clean state, removing all history past the last release?

I've spent about an hour googling now and can't find an answer to this specific question. Sorry if it seems redundant, it seems like such a simple task with no obvious answer!

Was it helpful?

Solution

To reset a local branch,

git branch -f master last-release

To reset a remote branch,

git push -f origin last-release:master

where last-release is the ref (commit id or branch) you want to reset master to.

(Neither of these affect your working tree; you can even do these from a bare repo, if you wish.)

OTHER TIPS

Sometimes, you post on Stack Overflow and you immediately figure it out a second later:

$ git reset --hard HEAD~9
$ git push --all --force

Now delete your local repo, re-clone.

Follow the below steps by checkout out specific commit and push the new branch and remove the branch protection rule and force push new branch into master and add back the branch protection rule which you deleted.

git log --online (to get the commit hash that you wish to revert) 
git checkout <commit-hash>
git branch <new-branch-name> <new-commit-hash> 
git push origin <new-branch-name>

# Goto Github and remove branch protection rule (for master)
git push -f origin <new-branch-name>:master

# Go back to Github and add back the branch protection rule (for master/main)

For remote repo

git push -f origin <last-release-number>:<branch_name>

For local repo

git reset --hard <last-release-number>

"last-release-number" must be the last commit you want to go back to

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top