How do I save a `git reset`

I have a few commits I want to get rid off. If I use the line below they disappear but if I do a sync or pull they return.

git reset --hard HEAD^1

How do I make sure the reset sticks on a remote branch named master?

  • Doing multiple pull requests on github
  • Git - How to color a failed push
  • git HEAD detached from after reset --hard
  • Understanding “git pull --rebase” vs “git rebase”
  • How can I run the makefile on every git branch
  • move my global .gitconfig file
  • Using git with subversion
  • Can I hide commits' time when I push to GitHub?
  • git init will not create git directories for me
  • Does git checkout -B perform a reset?
  • git remote different remotes for fetch and push
  • How merge resource files?
  • 3 Solutions collect form web for “How do I save a `git reset`”

    You need to push the new HEAD to the remote master.

        git push origin +HEAD:master
    

    This is very dangerous. Make triply sure you’re pushing the right thing. Anyone else who pulled the “bad” commits will have trouble.

    You’re not supposed to. Once pushed, your commits are published and shouldn’t be removed. Otherwise you could prevent changes someone else has made atop the original commit from being merged.

    Instead you should git revert the bad commit and push the new commit that removes it.

    Other answers will tell you how to “force push” but considering your question is tagged GitHub, your code should already be considerd public.

    Does anyone else have access to the remote repository? If so, you’ll need to apologize to them first, and warn them to back up their refs before their next pull, in case they have any work done since your bad push.

    Once you’ve reset-hard, push-force.

    $ git push -f
    

    Be warned: you’re forcing a push, meaning you’re throwing away commits from a remote (shared?) repository. This affects any and all other collaborators, in a term coined “pulling the rug from under people’s feet”.

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