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:
- git: Pushing changes in particular commit
- Point github repo to gh-pages not master
- Push to origin without having a local .git?
- Contributing to a repository on GitHub on a new branch
- Moved file location in Xcode 4, can't push to Github
- How to make eclipse remember ssh key passphrase?
A– I pulled this from the remote first and merged.
B– I pushed the merged commit..
C– I still had some merge conflicts, so i merged again and pushed.
Now the remote contains commit
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
C. Revert to commit
A and do the work again.
Kindly, let me know if there is a SAFE way to delete the commit
C without messing up other commits? Suggest other solution if possible.
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.
git checkout AYou will be in a detached state right now
git checkout -b new_branchVerify 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:
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
Now my commits B and C are deleted from the remote and the head points to A.