Git In-depth

Git In-depth Fixing Git Mistakes Solution

Check out a free preview of the full Git In-depth course:
The "Fixing Git Mistakes Solution" Lesson is part of the full, Git In-depth course featured in this preview video. Here's what you'd learn in this lesson:

Nina walks through the solution to Fixing Git Mistakes Exercise.

Get Unlimited Access Now

Transcript from the "Fixing Git Mistakes Solution" Lesson

>> Nina Zakharenko: Let's go through how we can fix mistakes. So I'm going to check out the exercise6 branch.
>> Nina Zakharenko: And I'm going to make some bad changes,
>> Nina Zakharenko: To my hello.template file. If I look at hello.template, we'll see I have some bad data in there. Well, no worries, I can just check out this copy.

[00:00:35] It's not committed yet. So I can just overwrite the working area with the last good copy in the staging area.
>> Nina Zakharenko: And I do that will git checkout --hello.template.
>> Nina Zakharenko: Now if I look at that file, we'll see that it's back to normal, great. If we want to check out a file from a specific point in time, if we wanted it to know what hello looked like before we made it a template, back when it was good old hello.txt, we can use git log.

[00:01:16] We use -- name status, and that -- follow that we learned about in the previous section.
>> Nina Zakharenko: And I'm going to simplify my log output oneline and pass in the name of the file. Great, we saw when it was added and when it was deleted.
>> Nina Zakharenko: And when it was renamed.

>> Nina Zakharenko: So we're going to copy this shaw.
>> Nina Zakharenko: So we're going to copy this shaw, and.
>> Nina Zakharenko: We want the commit before this one. So we can either say, this commit with a hat or we can just copy this commit. But if we want to get back that file,

>> Nina Zakharenko: We would do a git checkout. I'm gonna specify the commit before it got deleted. Because it's not present in that deleted commit. And I'm gonna check out hello.txt.
>> Nina Zakharenko: Look, it's back.
>> Nina Zakharenko: And it's in the staging area. At this point, we don't really wanna keep it there.

[00:02:42] So I'm just gonna run a reset.
>> Nina Zakharenko: Can someone tell me what HEAD is pointing to at this point? The last commit we made, right? So I'm gonna reset hello.txt back to that commit. And it doesn't exist in that commit. So now it's becoming untracked file, removed from the staging area.

>> Nina Zakharenko: What if I remove my hello.template?
>> Nina Zakharenko: If I run a git status, we'll see that it's now deleted. Well, let's make a commit.
>> Nina Zakharenko: If later on we decide that we actually that file was really important, and we need it back, I'm gonna show you how you can track it down.

[00:03:41] Track down to where you deleted it, and bring it back. For this, we're gonna use that handy diff-filter from earlier. And I'm gonna to pass in D for deleted.
>> Nina Zakharenko: And the name of my file. And we'll see here that autocomplete doesn't work because this file doesn't exist anymore.

>> Nina Zakharenko: Oops.
>> Nina Zakharenko: This is the kind of error that you might see if you forget those two double dashes. So I didn't specify that this was the start of our named arguments. So this ambiguous argument error is the kind of error you might see if you forget the So I'm gonna add that -- to my hello.template.

[00:04:33] And now we're gonna see where we can track it down and see what commit it was deleted from.
>> Nina Zakharenko: And that would be this commit. Oops, I'm gonna use --no-pager here. So that we can see the output in my terminal. Great, so this is the commit where we deleted hello.template.

[00:05:03] We can check it back out
>> Nina Zakharenko: By specifying the commit before, right? Doesn't exist in that commit. And now it's back. It's in our staging area. Gonna skip the solution for where we clear our repo. I'm sorry, clear our working area with g-t clean. I'm just going to manually delete this file so that my staging area is clean again.

[00:05:37] And I'm gonna talk about git reset. So at this point, I never committed my hello.template file after I deleted it. It's staged for commit. You can use git reset to unstage it. By default, if you don't specify an argument here, it just assumes HEAD.
>> Nina Zakharenko: Great, so now it's an untracked file.

[00:06:06] And that's because it was deleted in that last commit. If we use git reset on a file, on a file, instead of a commit, it works in mixed mode, so that means it's gonna copy it to the staging area. But it's also gonna keep your copy in the working area.

>> Nina Zakharenko: It's also possible to check out files from specific versions using git recent, but I'm going to move past that and talk to you about git reset --hard. So if you really messed up, you don't know how you got here and you just want your repository to look exactly like it did the last commit, you can use git reset --hard.

[00:06:59] It's gonna blow away our untracked files, anything in the staging area. So if I do that and I look at git status again, if I had
>> Nina Zakharenko: Mid changes, I'm sorry, hello.template is still here because it's not present in the current commit.
>> Nina Zakharenko: All right, if it was, it would be gone.

>> Nina Zakharenko: Remember that we can undo a git reset by using original HEAD. So if I look at my history.
>> Nina Zakharenko: And I see that I want to reset a repo back to where we
>> Nina Zakharenko: We placed our greeting tokens.
>> Nina Zakharenko: Say, I want to.
>> Nina Zakharenko: I'm sorry.
>> Nina Zakharenko: I want to look at the two previous commits.

[00:08:16] And I'm gonna do that with the -n2, or -n 2, not -2.
>> Nina Zakharenko: So, if I wanted to, if I did a git reset, let's say to here,
>> Nina Zakharenko: And now my HEAD pointer is pointing, it's exercise6 which is now pointing at this commit. If I wanted to undo that, I could do git reset ORIG_HEAD, and it'll get me back to where I need to be.

>> Nina Zakharenko: If we wanted to undo deleting hello.template, but we didn't wanna alter history with a reset, we can use a git revert.
>> Nina Zakharenko: So if I look at my log and find the commit where we deleted the hello.template, I wanna be able to revert it. I had some technical difficulties with exercise6 earlier.

[00:09:32] And I wanted to make sure that everyone had a good idea of how revert worked. So I'm gonna show that to you one more time. I'm currently on branch exercise6.
>> Nina Zakharenko: And if I look at my log, I'll see that I have a commit here where I deleted hello.template.

[00:09:58] And if I look, it's gone, and I wanna revert that commit. Let's say that I've already pushed it, I don't wanna rewrite history. I wanna get back hello.template but I wanna make a new revert commit. In order to do that, I would need to specify the commit where I deleted hello.template.

[00:10:25] And that's going to open an editor. And the editor will say revert and the commit name. It'll specify which commit it's reverting.
>> Nina Zakharenko: And notice here at the bottom, it'll also show what changes are going to be committed. It's a new file, hello.template. It's the opposite of the commit where we deleted the file.

[00:10:49] So now if I save this and exit, we'll see that one commit was created, the revert of delete hello.template.
>> Nina Zakharenko: And if I see, it's back here in my working area. And that's everything about reverts. If I look at the log again,
>> Nina Zakharenko: We'll see that that revert commit is there.

[00:11:23] It's a new commit. This commit where I deleted hello.template is the same. It hasn't changed. So I can safely push this without rewriting history.
>> Nina Zakharenko: And that's it for exercise6.