r/github • u/Life-Refrigerator200 • Mar 31 '25
How to "unpush" in GitHub...?
Hi all,
I would appreciate any help you could give me as this is for a course. Everything makes sense. I just went too fast, and now I can't figure out how to undo it. There is a remote repository called "main" (we shouldn't touch this), then we create a "working" branch. We clone to a local repository on our computer, then start going down a checklist. I accidentally didn't switch to "working" and ended up pushing to "main" and now can't get it undone. I was instructed to delete the created "working" branch and everything cloned to my computer, but it still isn't correct. Help help!
In the screenshot, you can see where it says "2 days ago" for about.html, contact.html. and customers.html. Those should be 1 year like the rest. Graph you will also see where the changes are made to "main" and not "working". I've already deleted other branches. Thank you!


8
u/Alternative-Fail4586 Mar 31 '25
Everyone else seem to have answered your question but I came to say that you or whoever is the owner of the repo should setup branch protection on the main branch so you can't push to it. It's an easy misstanke to make
1
u/Life-Refrigerator200 Mar 31 '25
Main was created by the university beforehand but good to know for the future. Unless it can be a local protection too. Thank you :)
1
6
u/DevilStuff123 Mar 31 '25
first back up the changes you made, because the commands I just sent you are about to undo those changes.
This command will bring you back to the previous commit. make sure you’re on main: git reset —hard HEAD~1
Then, this command will forcibly push the changes: git push —force origin main
1
u/Life-Refrigerator200 Mar 31 '25
I think I tried that but could have made a mistake. It's just me, so I can delete everything. Thank you!
1
u/rish_p Mar 31 '25
reading this you learned more about git then the course/lecture/plan intended
good for you, kudos
1
1
2
u/armahillo Apr 01 '25
No idea how to do it in your tool, but from the CLI (assuming you just pushed it up):
# Ensure you're on main
git checkout main
# Rollback to the previous iteration, before you did your commit
git reset HEAD~1
# Add all the files that were staged in that commit
git add .
# Put them in the stash to replay later
git stash save
# Force-push the current state back up to the remote repo
git push -f
# Switch back to your working branch
git checkout working
# Ensure working is sync'd with main
git rebase main
# Pop the stashed code from earlier, replaying it onto working.
git stash pop
# Commit these files onto the working branch
git commit -am "Replace development work onto working branch"
# Push the branch back up
git push -f1
1
1
1
u/ravinggenius Apr 01 '25
You need to find the commit that main should point to. Then run git push --force <commit-id>:main. This will reset Github's main back to the correct commit. This doesn't remove the commits, but it does reset the main branch to the state it was in. If you still care about these commits, you can incorporate them into your working branch, or checkout a new branch with them.
However if anyone has pulled these changes to your main branch, this is going to mess them up. Obviously you should never do this on a "real" project (especially on main), but it's fine (in my opinion) for a learning or side project with not many people contributing. You should still try to communicate what's happening so they can expect it.
-1
u/Livid-Ad-2207 Mar 31 '25
Get GitHub Desktop, right click on the commit and revert changes, it's the cleanest way to do it.
0
Mar 31 '25
[deleted]
16
u/Life-Refrigerator200 Mar 31 '25
Thank you!
5
u/wick3dr0se Mar 31 '25 edited Apr 01 '25
--softinstead of--hardif you want to keep the commit locally3
u/Poat540 Mar 31 '25
Or just omit, I use this command all the time when I forget to pull from my personal project that bumps the version in the repo…. One day I’ll remember
1
u/wick3dr0se Apr 01 '25
I feel it. Besides using the command above frequently, I am also a heavy
git commit --amendabuser1
u/Life-Refrigerator200 Apr 01 '25
I’ll need to learn that one. Fortunately this ended up being a ‘do you have a brain?’ course so did they didn’t really cover potential issues or anything really lol
2
u/bhechinger Apr 03 '25
Be very, very, very, very super extra careful when doing this. You'll have to force push. If you're an idiot like me you'll cock it up and lose 3 months of work. Because you're also an idiot who didn't back any of that shit up anywhere else.
RIP my nixos config. 🤣
12
u/Patrick-T80 Mar 31 '25
I add to this, the OP can set protection rule based on branch where the push is forbidden
4
u/ziksy9 Mar 31 '25
This should be the default in any multi developer workflow. Should also require a code review.
2
u/Difficult-Court9522 Mar 31 '25
It doesn’t work! GitHub never forgets. Your commit is still there to find
1
u/Mchlpl Apr 01 '25
Yeah. It's not really deleting a commit, just changing a commit to which 'main' branch points. This is usually enough though.
2
u/Kaylebor Apr 01 '25
Small nitpick: it technically does not delete the commit, what that does is “re-tag” the previous commit (
HEAD~1, orHEAD^also works), setting it as the tip of the branch (in this casemain)
—hardalso means “change files to the same state as commitHEAD~1; if you want to keep the changes while still resetting which commit is the latest,—softdoes just that, and leaves the “bad” commit changes in the Staging area. From there,git restore —staged .would remove them from the commit, leaving you exactly where you probably want to be.If you have pushed the bad commit, then from there you can do
git push —force-with-leaseto also reset the remote; although onmain, if this is work-related, I would contact a colleague first and explain the situation.Finally, yeah, protecting the
mainbranch is something we should all do :)1
Mar 31 '25
What about all the other people who have local repos? This is a recipe for endless problems. The only time you should ever putz around like this is if it hasn't been pushed and is entirely local.
It's much better to accept the mistake and roll forward to the previous commit. Let people know you did something stupid, fixed it, and they should pull the main branch to stay up to date.
1
u/Unbelievabob Mar 31 '25
OP appears to be using gitlab - https://docs.gitlab.com/user/project/repository/branches/protected/#add-protection-to-existing-branches
1
u/No_Package_9237 Apr 02 '25
Two options :
remove the commits from the remote branch (that means rewrite the history and you might fuck up even more by removing new commits from coworkers). I would STRONGLY advise AGAINST doing so on a shared branch. If you follow this path, I suggest you do a git push --force-with-lease instead of git push --force.
use git as it is intended : an append only log. That means doing a git revert to create a new commit (you can revert multiple commits at once also) that revert previous one. No need to force the push anymore and you'll keep track of this easy mistake forever (an opportunity to post mortem and activate branch protection on github for instance)
1
1
u/No_Package_9237 Apr 02 '25
And also, learn that pushing to trunk is not bad per se, so you've just been ahead of time in term of modern practice ! No need to be ashamed here.
54
u/madonkey Mar 31 '25
The term you're looking for is "revert" if you want to keep the git history, or "reset" if you don't.
Have a read of this beginner friendly post: https://www.freecodecamp.org/news/git-revert-commit-how-to-undo-the-last-commit/