4

My fork is origin and the one I fork from is upstream

I have to delete a commit from my fork, but I would like to update the upstream repo in case other folks need to fork it (it's just me at the moment).

So far I've done the following:

git checkout MYBRANCH
git reset --hard <SHA_COMMIT_HASH>
git push origin MYBRANCH

As of now my upstream repo is protected (it will at some point), so I tried to push to that repo but I got the following message. I tried forcing it but it didn't work either. I doublechecked and the branch is not protected.

hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
7
  • if push -f not working, branch or repository is protected and force is disabled, otherwise it should work
    – Divisadero
    Apr 13, 2018 at 12:28
  • Using Github Enterprise if that makes a difference.
    – sdot257
    Apr 13, 2018 at 12:29
  • Are you trying to merge the MYBRANCH to some other branch upstream or MYBRANCH is the only branch?
    – nak
    Apr 13, 2018 at 12:38
  • @nak yes, I want to merge it into upstream since that's where I forked from and others will as well.
    – sdot257
    Apr 13, 2018 at 12:39
  • Are other developers using MYBRANCH or is it just yours?
    – nak
    Apr 13, 2018 at 13:03

1 Answer 1

1

You have already reset the commit. You can proceed with 2 approaches

  1. Force with lease Push - git push origin --force-with-lease MYBRANCH. This will override the commit and remove it from the remote but get rejected if the remote repo has been updated since your last pull. This is not technically necessary in your case (your branch, only you committing to repo) but it's a good habit to get in to.
  2. Create a new branch from MYBRANCH. Push to remote and merge it to MYBRANCH upstream.

I usually follow the rules mention below when it comes to deleting commits

Deleting commits can be done in two ways

  1. git reset - destroys commit making your remote branch and local branch out of sync
  2. git revert - create a new revert commit which can be easily pushed to the remote repository

Use RESET if you are working on your local branch and not collaborating it with other developers. Since Reset destroys the commit, your reset commit might be available at other developer's repository and he can accidentally push it again.

Use REVERT if you are working on a collaborative branch. In this case, a new revert commit will be added. So even if someone has the commit the revert will override it.

Adhering to these rule will prevent any issues related deleting a commit in future.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.