How to delete a remote git commit HEAD?

I messed up a lot of code with git merge and also lost my work in the process.

I am in this situation:

  • commit A – I pulled this from the remote first and merged.
  • commit B – I pushed the merged commit..
  • commit C – I still had some merge conflicts, so i merged again and pushed.

Now the remote contains commit C as HEAD. It is totally flawed and also, I lost my code. The only way I can solve this is I want to delete the commit B and C. Revert to commit A and do the work again.

Kindly, let me know if there is a SAFE way to delete the commit B and C without messing up other commits? Suggest other solution if possible.

Thanks..

  • conflicts on git pull brings uncommited remote changes
  • Managing a git with sub repositories
  • How to set develop branch as default in Github instead of master?
  • Git push: username, password, how to avoid?
  • Git - How to selectively apply changes from one branch to another?
  • Multiple instances of app and configurations
  • How can I successfully checkout my coworker's repository?
  • Why does git checkout create a branch which tracks a remote branch?
  • 2 Solutions collect form web for “How to delete a remote git commit HEAD?”

    You can checkout commit A, create a new branch out of it, and then push this branch and use it in future.

    Basically, do:

    • git checkout A You will be in a detached state right now
    • git checkout -b new_branch Verify all your changes are there as needed
    • git push origin new_branch

    Now you can continue working on this new_branch.

    In case you need to overwrite the older branch itself, you have two options

    • Either force push your branch in the last step above using
      • git push origin new_branch:old_branch_on_origin
    • Or reset the HEAD on branch locally and force push it
      • git checkout old_branch
      • git reset HEAD --hard A
      • git push origin -f old_branch

    The problem with the second approach above is that if some other developer has pulled in your changes and started working on them, the branch history on his machine could get out of sync because of the forced push.

    I found the solution from this:

    http://lrotherfield.com/blog/delete-remote-git-repo-to-specific-commit/

    STEP 1 : For resetting head locally,(consider A as the commit id)

    git reset –hard A

    STEP 2 : For resetting head in the remote

    git push -f origin A:master

    Finished..

    Now my commits B and C are deleted from the remote and the head points to A.

    Git Baby is a git and github fan, let's start git clone.