It can happen to everyone (especially at 4 am 😴) to push a commit and immediately realize that it shouldn't be done. Why mess with your commit history with other commits when you can just take one (or more) back steps?
As usual, for simplicity, let's establish a simple situation that allows us to easily understand the matter: you have connected your local project to a remote repository on GitHub via Git. You only have one
README.md and obviously, the concept you are about to learn is independent of the type and number of files involved.
README.md that you already pushed to the remote repository contains:
Well, you must know that in the hidden
.git folder 👻, among the various files, there is one called HEAD. It is a pointer to the commits. When it is not followed by anything else it is referencing the last commit made. Otherwise, by posting a ~ (tilde) and an integer to it you can point to previous commits:
HEAD- the current commit
HEAD~1- one commit ago
HEAD~2- two commits ago
And so on...
Notice how HEAD lays on the most recent commit
As you have certainly noticed, an ID is associated with each commit. Choose the commit you want to go back to, copy the ID, and type
git reset [commit_ID]
Now in the staging step there are the files added or modified at that point and you are free to modify and commit them again.
One last note, in case you want to go back to a previous commit and have all the changes made completely removed, both from the IDE and from the staging step, you can add the
git reset --herd [commit_ID]
This command causes the HEAD pointer to now be associated with the newly obtained state. Beware: in fact, by doing so you have lost the various commits made after the one you just hardly returned to.
There is one last thing any programmer looking to improve should know, and that is the concept of forking.
What do you say, tiger 🐯?
Okay, let's go [VERY_VERY_SOON]
🐤 twitter: https://twitter.com/did0f