Do you know that feeling? Oh no, what have I done? Every now and then it happens to everybody.
You need to undo the changes that you have just committed. Now the ability to undo a
change on GitHub depends on the current state of the modification. If a file is staged but
not yet committed, it's easy. Use git restore minus minus staged on the filename. So git
rm filename git status will show you the removed filename which is still in the staging area
and to get it out of the staging area back in the normal environment, git restore will
do it for you. If a file has already been committed, it's a bit harder. You need git
log and identify the commit by the commit message. And next you use the commit hash
or a part of it to revert the transaction using git checkout. And do make sure if you
are going for this procedure that you revert to the last commit where you still had the
file. Not always easy to find out which that was. Let me demonstrate. So I'm in my git
repo and I'm going to git rm group. And then git status is giving me a deleted file. Next
I'm going to use git commit minus m removing still only a message and now the file has
been deleted. And git status is showing nothing anymore. Now to make it even worse, I'm going
to use git push to push the change to the remote. And now I want to use a git log minus
minus followed by the name of the removed file. So git log minus minus group is showing
what? Well, it's showing this guy. Here we have the commit. The commit message was
removing. Do you see how important these commit messages are, by the way? And here we have
the previous one with the minor changes. So that is the one that I'm going to need. And
in order to repair it, I'm using git checkout 34F0. Enough of the last commit ID should
do it. Oh, no, I have a detached head. What is going on? Well, the problem is that I have
repaired the file and I have not used any branches and git doesn't like that very much.
And we need to fix it. How do we do that? Well, first, I'm going to use git branch minus
minus show current. And that is showing me nothing. And why is it not showing me nothing?
That's because I'm detached. I'm not in a current branch. And that is why git is confused.
Now I'm going to use git lev log. And there we can see that in the reference log, I was
moving from main to 34F0. So from here, I'm learning that the last branch I was in was
main. And now I'm going to use git switch main. And there it is telling me that the
previous branch had minor changes, and it switched to branch new. And when I use ls,
then I can see my... Okay, so what is going on? Well, I'm back in my main branch, but my
file is still in the old branch. So what is the hash of the old branch? That's 34F0. And I
need to get my file from 34F0 into my current environment. And how am I going to do that?
Well, I'm going to use git restore. Minus minus source is 34F0. Minus minus group, because
that is the name of the file that I want to restore. Then I'm using git add group to get my
file back and git status to see what is going on. And there we can see that it's back as a new
file. And now that it is back as a new file, I can use git commit to finally get it definitely
back. So git commit minus m getting the file back, or maybe more specific, getting the
accidentally removed file back. And now my file is back. And the only thing I still have to do is
my my git push, as you can see, and it's now up on the remote as well.