I often mess up. Sometimes badly π€―. git reset
is my friend.
Here's the current state of the Metacat repo:

Imagine I commit a file to develop. When I look at the log, I see its gotten pretty messy, because the merge commit in main wasn't committed into develop. This makes for a messy tree.

What if I want to go back and clean it up? First, I can do a soft reset to return to the state before I did my commit.
In this case, the develop
branch returns to where it was, and my changes are left uncommitted in the README file.
I can run the command git reset --soft ad29acc8
to bring the develop branch back to where it was.

Note that my changes are now uncommitted in the README.md file:

So, now I can do what I shoud have done before, and merge main into develop to create a clean starting point for new changes.

π π π Now I have a clean tree. Note that both develop
and main
point at the same commit hash:

And I can redo my commit (git add README.md; git commit
), which results in a linear history with my changes to README:

Now I have a clean, linear history, but I added some garbage into develop. What if I want to just go back? This is a job for
git reset --hard
, which moves the branch back to the original commit and discards all of the changes.
Be careful, you can lose stuff here with no way to get it back!

Et voila! We're back where we started. π It's like we were never even here. (Not really, though, appearances can be deceiving.)

One last warning. git reset rewrites history. So, this is super useful before you push. Once you've pushed, your history is shared for everyone to see, you can't really use reset like this, and you'll need to be more strategic to cover your tracks. Don't rewrite history in shared repositories, it messes with everyone's day.