2

I have tried the rebase, per these instructions (How can I remove a commit on GitHub?), and nothing seems to change.

On computer A, my work system, I had the most recent version of my code. I pushed it to my repo and came home.

Here, with computer B, at home, the last time I'd updated the code was months ago. Apparently I'd made a change or two back in the day (nothing important) and thus had merge conflicts. Used github desktop to do a 'fetch' and it tried to merge; Due to not understanding how merging works, I screwed something up (turns out the merge didn't include lots of needed files)... but didn't know it until it was too late. I then tried to do another github desktop fetch/pull so everything would be sync'd and I wouldn't have this problem again after working from my home system.

I then realized I was missing files and other files were old/screwed up.

On Github, if I look at the commit history... the correct files are one commit back.

All I want to do is blow away my current local repo (which doesn't have any of the current, good, files anyway) and tell github to delete the most recent commit. Then I'll clone and start fresh on the home system.

But I can't for the life of me find a way to delete a commit on github.

EDIT: Given the insistence of @Alex Harvey that the linked instructions working, I went back to the link. Having no luck with the first answer (nothing in the local repo changed) I went with the other option that has 800+ upvotes. I used the command: git push -f origin HEAD^:master

Unfortunately, this deleted about 100 of the most recent commits from my github repo. It looks like it deleted every commit on github since I last sync'd my home computer with the github repo - many months ago.

Help?

EDIT2: To be clear, the system I am has not been used or sync'd to the github repo in months. The only commit locally in the past few months was the 'merge' commit I did that started this whole mess. I suspect that moving back one commit and then forcing a push resulted in the github repo going back in time to how this system looked months ago.

EDIT3: I went to the work system and did a "git push origin master --force" and recovered my work.

lowcrawler
  • 6,777
  • 9
  • 37
  • 79
  • Those instructions you followed should definitely allow you to delete a commit on Github. Perhaps explain why that's not working for you? – Alex Harvey Jan 31 '19 at 05:32
  • @AlexHarvey, I added an edit. Things have gotten worse. To answer here: when I ran the rebase command and deleted the line... nothing in my local repo changed... so I wasn't sure what to push. – lowcrawler Jan 31 '19 at 05:40
  • It's not possible that those instructions alone did that. You must have force-pushed a local copy that also had 100 commits missing. – Alex Harvey Jan 31 '19 at 05:40
  • @AlexHarvey - I added another edit. – lowcrawler Jan 31 '19 at 05:43
  • Yeah. I am not sure how I can help here. I assumed you were following the instructions upvoted 1078 times. Git push --force will definitely overwrite the Github copy with your local copy. – Alex Harvey Jan 31 '19 at 05:49
  • Do you or does someone else have a recent copy of that repo? – Alex Harvey Jan 31 '19 at 05:50
  • I did the 1000 upvote one, nothing happened (I changed the line, nothing changed after the rebase) ... so I tried the other. I've still got a copy on the work system, so I'm not freaking out yet. ;) – lowcrawler Jan 31 '19 at 05:53
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/187621/discussion-between-alex-harvey-and-lowcrawler). – Alex Harvey Jan 31 '19 at 05:53

1 Answers1

6

If you really wanted to delete the HEAD (latest) commit on a branch in GitHub, then you could do this:

git checkout your_branch
git reset --hard HEAD~1
git push --force origin your_branch

However (italics perhaps not even enough emphasis), most of the time you don't want to use this option. The reason is that removing the HEAD commit and then force pushing effectively rewrites the public history of your_branch. This, in turn, can cause problems for anyone else who is also sharing this branch.

If you were certain that you were the only one using this branch, then a hard reset might be justified. Otherwise, I recommend using git revert:

git revert HEAD

This will actually add a new commit on top of your_branch, which will functionally undo the previous HEAD commit. This option is safe even if the branch be shared among more than one person.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360