Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to recover after pushing to master by mistake

Tags:

git

I make this mistake quite frequently and I am wondering if there's a better solution, and also whether my current solution is risky.

Here's the workflow

  1. git checkout master
  2. git pull
  3. git submodule update --init --recursive
  4. (edit, debug, test)
  5. git commit -a
  6. git push origin HEAD:refs/for/master
  7. (more edit, test, etc.)
  8. git commit -a
  9. gitk
  10. (swearing)
  11. git rebase -i HEAD~2 # pick and squash my two commits together
  12. git checkout -b task_id
  13. git checkout master
  14. git rebase -i HEAD~2 # pick previous user's commit, delete my own

My mistake is that somewhere around step 5 I should be doing something like step 12 to create a new branch to keep this work separate from every other task, to allow me to easily come back to it or rebase different patches in the desired order or whatever. Instead, I am pushing new code onto the master branch, and around about step 10 I realize that master is no longer pointing at the latest and greatest version of the code that the company has approved, but my own efforts. So after squishing my own commits into one with an interactive rebase, I'm doing another interactive rebase to get master pointed back where it should be -- namely, someone else's work that has been through the review process.

Edit to clarify: When I "push to master" this doesn't publish my changes to everyone else. There's some commit hook magic that creates a Gerrit code review task and only when approval is obtained from that is my commit merged into the remote repository. Apologies for misleading you all. My concern here is only for my local repo.

My question is twofold. How bad is this practice, in terms of risk, and how could it be improved upon? I'm not asking for help in avoiding the initial error. I think I just need to be more careful about which branch I'm pushing to. The question is how best to point master at the previous commit without disturbing/risking/losing anything.

like image 952
Tim Randall Avatar asked Oct 31 '25 13:10

Tim Randall


1 Answers

If you have only pushed a single commit, you can do the following:

git checkout master
git reset --hard HEAD~
git push -f

This will restore master to the previous commit. If you made more commits, you can replace HEAD~ with any other commitish (a SHA1 hash, a branch name, a tag name, etc.).

My mistake is that somewhere around step 5 I should be doing something like step 12

I suggest that at Step 4, before you do any coding at all, that you create a new branch. There should be no ambiguity here. Typical best practice is to create a new feature branch in order to keep on going work separate and only merge it into master once the work is complete. Committing half-complete work on master will make your software unstable and difficult to release.

like image 75
Code-Apprentice Avatar answered Nov 02 '25 02:11

Code-Apprentice



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!