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,180 --> 00:01:55,199 what? Well, it's showing this guy. Here we have the commit. The commit message was 18 00:01:55,199 --> 00:01:59,760 removing. Do you see how important these commit messages are, by the way? And here we have 19 00:01:59,760 --> 00:02:05,360 the previous one with the minor changes. So that is the one that I'm going to need. And 20 00:02:05,360 --> 00:02:14,559 in order to repair it, I'm using git checkout 34F0. Enough of the last commit ID should 21 00:02:14,559 --> 00:02:22,759 do it. Oh, no, I have a detached head. What is going on? Well, the problem is that I have 22 00:02:22,800 --> 00:02:28,440 repaired the file and I have not used any branches and git doesn't like that very much. 23 00:02:29,039 --> 00:02:37,440 And we need to fix it. How do we do that? Well, first, I'm going to use git branch minus 24 00:02:37,440 --> 00:02:43,919 minus show current. And that is showing me nothing. And why is it not showing me nothing? 25 00:02:43,960 --> 00:02:49,800 That's because I'm detached. I'm not in a current branch. And that is why git is confused. 26 00:02:49,839 --> 00:02:56,800 Now I'm going to use git lev log. And there we can see that in the reference log, I was 27 00:02:56,800 --> 00:03:03,919 moving from main to 34F0. So from here, I'm learning that the last branch I was in was 28 00:03:03,919 --> 00:03:10,360 main. And now I'm going to use git switch main. And there it is telling me that the 29 00:03:10,360 --> 00:03:17,000 previous branch had minor changes, and it switched to branch new. And when I use ls, 30 00:03:17,279 --> 00:03:24,279 then I can see my... Okay, so what is going on? Well, I'm back in my main branch, but my 31 00:03:24,279 --> 00:03:34,960 file is still in the old branch. So what is the hash of the old branch? That's 34F0. And I 32 00:03:34,960 --> 00:03:41,600 need to get my file from 34F0 into my current environment. And how am I going to do that? 33 00:03:41,600 --> 00:03:54,960 Well, I'm going to use git restore. Minus minus source is 34F0. Minus minus group, because 34 00:03:54,960 --> 00:04:01,360 that is the name of the file that I want to restore. Then I'm using git add group to get my 35 00:04:01,360 --> 00:04:09,119 file back and git status to see what is going on. And there we can see that it's back as a new 36 00:04:09,119 --> 00:04:15,520 file. And now that it is back as a new file, I can use git commit to finally get it definitely 37 00:04:15,520 --> 00:04:24,959 back. So git commit minus m getting the file back, or maybe more specific, getting the 38 00:04:25,000 --> 00:04:36,559 accidentally removed file back. And now my file is back. And the only thing I still have to do is 39 00:04:36,559 --> 00:04:42,399 my my git push, as you can see, and it's now up on the remote as well.