1
00:00:00,000 --> 00:00:12,000
Do you know that feeling? Oh no, what have I done? Every now and then it happens to everybody.

2
00:00:12,000 --> 00:00:16,879
You need to undo the changes that you have just committed. Now the ability to undo a

3
00:00:16,879 --> 00:00:22,160
change on GitHub depends on the current state of the modification. If a file is staged but

4
00:00:22,160 --> 00:00:28,240
not yet committed, it's easy. Use git restore minus minus staged on the filename. So git

5
00:00:28,240 --> 00:00:33,840
rm filename git status will show you the removed filename which is still in the staging area

6
00:00:33,840 --> 00:00:39,080
and to get it out of the staging area back in the normal environment, git restore will

7
00:00:39,080 --> 00:00:44,439
do it for you. If a file has already been committed, it's a bit harder. You need git

8
00:00:44,439 --> 00:00:49,880
log and identify the commit by the commit message. And next you use the commit hash

9
00:00:49,880 --> 00:00:56,119
or a part of it to revert the transaction using git checkout. And do make sure if you

10
00:00:56,119 --> 00:01:00,840
are going for this procedure that you revert to the last commit where you still had the

11
00:01:00,840 --> 00:01:07,360
file. Not always easy to find out which that was. Let me demonstrate. So I'm in my git

12
00:01:07,360 --> 00:01:17,839
repo and I'm going to git rm group. And then git status is giving me a deleted file. Next

13
00:01:17,839 --> 00:01:26,559
I'm going to use git commit minus m removing still only a message and now the file has

14
00:01:26,559 --> 00:01:33,160
been deleted. And git status is showing nothing anymore. Now to make it even worse, I'm going

15
00:01:33,160 --> 00:01:41,440
to use git push to push the change to the remote. And now I want to use a git log minus

16
00:01:41,440 --> 00:01:49,160
minus followed by the name of the removed file. So git log minus minus group is showing

17
00:01:49,199 --> 00:01:55,760
what? Well, it's showing this guy here we have the commit. The commit message was removing.

18
00:01:55,760 --> 00:02:00,320
Do you see how important these commit messages are, by the way? And here we have the previous

19
00:02:00,320 --> 00:02:06,160
one with the minor changes. So that is the one that I'm going to need. And in order to

20
00:02:06,400 --> 00:02:16,240
repair it, I'm using git checkout 34F0. Enough of the last commit ID should do it. Oh, no, I

21
00:02:16,240 --> 00:02:25,199
have a detached head. What is going on? Well, the problem is that I have repaired the file and I

22
00:02:25,199 --> 00:02:31,720
have not used any branches and git doesn't like that very much. And we need to fix it. How do

23
00:02:31,720 --> 00:02:41,360
we do that? Well, first, I'm going to use git branch minus minus show current. And that is

24
00:02:41,360 --> 00:02:46,199
showing me nothing. And why is it not showing me nothing? That's because I'm detached. I'm not

25
00:02:46,199 --> 00:02:53,520
in a current branch. And that is why git is confused. Now I'm going to use git lev log. And

26
00:02:53,520 --> 00:03:01,639
there we can see that in the reference log, I was moving from main to 34F0. So from here, I'm

27
00:03:01,639 --> 00:03:08,679
learning that the last branch I was in was main. And now I'm going to use git switch main. And

28
00:03:08,679 --> 00:03:15,000
there it is telling me that the previous branch had minor changes, and it switched to branch new.

29
00:03:15,399 --> 00:03:21,440
And when I use ls, well, I'm back in my main branch, but my file is not. My file is still in

30
00:03:21,440 --> 00:03:30,440
the old branch. So what is the hash of the old branch? That's the 34F0. And I need to get my

31
00:03:30,440 --> 00:03:37,240
file from 34F0 into my current environment. And how am I going to do that? Well, I'm going to use

32
00:03:37,240 --> 00:03:50,960
git restore minus minus source is 34F0 minus minus group, because that is the name of the file that

33
00:03:50,960 --> 00:04:00,800
I want to restore. Then I'm using git add group to get my file back and git status to see what is

34
00:04:00,800 --> 00:04:06,839
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

35
00:04:06,839 --> 00:04:15,759
can use git commit to finally get it definitely back. So git commit minus m getting the file back

36
00:04:15,759 --> 00:04:29,119
or maybe more specific, getting the accidentally removed file back. And now my file is back. And

37
00:04:29,119 --> 00:04:36,559
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

38
00:04:36,600 --> 00:04:37,119
as well.

