4

Small team. A colleague pushed to origin:master by mistake. He has reset his local repo but cannot push -f to Github because repo is protected.

I have fetched the repo but not merged his errant commit into my local master...yet.

How can I, assuming I can push -f to origin, reset origin on Github so that it reflects the state it was in before his mistake?

$ git push origin 2860a4c:master
To github.com:example/myproj.git
 ! [rejected]        2860a4c -> master (non-fast-forward)
error: failed to push some refs to 'git@github.com:example/myproj.git'
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.

Do I really need to integrate the bad commit (with git pull) before I can then, I assume, reset hard 2860a4c and then push -f origin?

I just don't want to make things worse.

Meltemi
  • 36,348
  • 48
  • 182
  • 274
  • 2
    Possible duplicate of [How to revert Git repository to a previous commit?](http://stackoverflow.com/questions/4114095/how-to-revert-git-repository-to-a-previous-commit) – Eli Sadoff Oct 25 '16 at 22:14
  • I know there are a lot (too many?) of posts here about reverting git commits. However, I haven't found one that clearly explains how to remove (reset) last commit on Github. I suppose I could `revert` the commit, but I'm trying to find a way to just undo that last Github commit, without blowing up local repos, if possible. – Meltemi Oct 25 '16 at 22:18
  • 1
    Without `-f` you can not do it. You need to revert that commit using `git revert` but that will be the part of your history – Anonymous Oct 25 '16 at 22:30
  • Do you have permission to do `-f` – Anonymous Oct 25 '16 at 22:55
  • 1
    "Assume I have permission to `push -f`" so yes, i have permission (or can grant it for the time being). – Meltemi Oct 25 '16 at 23:41

3 Answers3

15

Below are the steps you may do, assuming you have permission for git push -f.

On your machine, do:

# Step 1: Take the changes from remote
git pull

# Step 2: Note the commit to which you want for restoring your repo to 
# using `git log`. Say the commit id is "x". 
git log

# Step 3: Do hard reset for that commit. 
#         ** NOTE ** All the changes after the commit "x" will be removed
git reset --hard x    # where x is the commit id

# Step 4: Push to remote
git push -f

Then on collegue's machine, do step 1 to step 3 and then do git pull to merge the remote changes


In case you do NOT have permission for git push -f, do:

git pull

git revert <commit id>   # may get it via "git log"

git push

With git revert, changes from the reverted commit will be removed, but this commit will remain in the commit history.

Anonymous
  • 40,020
  • 8
  • 82
  • 111
2

If you can push -f, than do it. If the state you want is one commit before the top, you can run

git pull
git reset --hard @~1
git push -f

Be sure everyone else in the team is in sync, so they don't lose any work.

choroba
  • 200,498
  • 20
  • 180
  • 248
2

You don't need to pull bad commit, therefore git reset is not needed neither. If I understood the question correctly git push --force should be sufficient. It will bring remote's state to yours.

In case you've already pulled bad commit, use git reset --hard + git push --force as here: https://stackoverflow.com/a/37145089/1663197.

AleXoundOS
  • 113
  • 1
  • 3
  • 7