Today, I messed up my repo. I was moving files back and forth between two repo’s and got confused about which I was on at the moment. So, on the wrong repo I added some files, committed, and pushed.
To make matters worse, I didn’t know how to get back to my previous commit. I turned to AI (Llama3.1 in this case) and I got some bad information, which I followed and then I had a merge and another commit on top of my old commit but still my files were in the wrong state.
So, I needed to get back to that previous good commit. Let me jump forward in time to the point of this article…
In my case I was only using the main branch and I had the appropriate permissions to push to that branch.
Here are the commands I used to get back to the previous commit and get out of the mess I had made.
git reset --hard [hash]
git push -f origin main
This can have significant consequences, especially if other collaborators are working on the main branch, as their changes will be lost. Use force push (-f) with caution, and ensure you communicate with your team before doing so to avoid unintended data loss.
If you don’t work from main
you can also do the same reset on a branch and push that instead. This has the positive side-effect that history is retained. This will also set you up so that you can use a GitHub pull request if that’s part of your workflow. Here are the steps to do the same thing on a branch:
git checkout -b my-new-branch
git reset --hard [hash]
git commit --allow-empty -m "Revert to commit e13324f"
git push origin
From there you can do a pull request between main
and the new branch (main <- my-new-branch
).
Before I got there I need to learn a few things. I returned to AI to get ideas on how to get back to commit c84bd
. Apparently I like punishment. This time, however, I decided to ask three different models. Here’s my prompt:
I deleted a file and committed it. I then made some other changes that were wrong on top of it and merged and committed those. Basically I’ve made a mess of things. I need to revert back to a previous commit, c84bd. Then I want to push that so that my main branch matches that old commit. How do I do it?
I got back three answers, each of which look like they might work. My choices seem to be:
git reset --hard c84bd
git revert c84bdb
git checkout c84bdb; git reset --hard c84bdb
The AI suggested git reset --hard c84bd
but I didn’t think I actually want to reset --hard
as I have other local files that are untracked and I want to keep those. I thought that --hard
would get rid of those. When I asked a follow-up question, the AI’s couldn’t agree on if these files would be deleted or not.
So, I copied my entire directory and gave it a try. Untracked files DO NOT get removed when you do a reset with --hard
.
In addition I found myself in a similar situation at work not long after writing this post. There we have branch protection enabled and so I couldn’t push to main
like I had done on my personal repository. I did some additional testing to find out how to do this to a branch and documented that above.
Written by Joel Dare on August 30, 2024 and updated on October 1, 2024.