Check out a free preview of the full Git In-depth course

The "Git Clean & Reset" Lesson is part of the full, Git In-depth course featured in this preview video. Here's what you'd learn in this lesson:

After discussing git clean command, which clears working area by deleting untracked files, Nina reviews git reset command, which removes the file from the index while keeping it in the working directory. Nina answers questions from students.

Preview
Close

Transcript from the "Git Clean & Reset" Lesson

[00:00:00]
>> Nina Zakharenko: Git Clean. Someone was asking earlier, I had a student question, how do I clean up my working area?
>> Nina Zakharenko: Git Clean does that for you. It clears your working area by deleting untracked files. Again, this operation can't be undone. But this operation is a little bit friendlier.

[00:00:19]
If I run Git Clean with no arguments, it gives me a warning. I can use the --dry-run flag, and that will list what's going to be deleted. And then I can do the -f flag to actually do the deletion. I can also specify -d to clean both files and directories.

[00:00:42]
>> Nina Zakharenko: So Git Clean --dry-run, it's telling me that it would remove a -note.txt, that's a file on my working area that's not committed. If I actually wanna do it, I do, git clean -d and --dry-run. That would show me, I'm sorry, if I don't pass the -f flag, it would show me what files and directories it would delete.

[00:01:08]
So I have this scratch directory here. Now, I actually run it with git clean -d -f, and it goes ahead and deletes those two files, the file and the directory. And we'll see now that, if I run and get status, my working area is clean.
>> Nina Zakharenko: Git reset.

[00:01:29]
This is a big one. I find that people tend to Google, how do I undo the last command, and just copy and paste the answer from stack overflow. But understanding reset will really supercharge your workflow. But you have to know that it's another one of those commands that performs different actions, depending on if it's run with a path or without a path.

[00:01:53]
Depending on the arguments, the actions are different. But by default, git performs a git reset --mixed.
>> Nina Zakharenko: Understanding reset is really important, because all it takes is one git reset --hard. You weren't sure what you were running, and all of a sudden, you overwrote your changes. You had some unsaved changes, and now you wanna swear off git forever.

[00:02:21]
>> Nina Zakharenko: But we’re gonna really understand what git reset does. And once you figure that out, you’re never gonna be afraid to use it again.
>> Nina Zakharenko: A big difference between git reset and git checkout is, git checkout will move the head,
>> Nina Zakharenko: But the branch stays where it was.

[00:02:45]
Git reset moves the head and it'll move the branch reference, meaning your branch is now modified. Whatever it was pointing to before is going to be different. Now for commits, git reset moves the head pointer, and it will optionally modify files.
>> Nina Zakharenko: But if you pass a file path, it's not going to move the head pointer, it's just going to modify the files.

[00:03:17]
This is a lot to take in, and I'm seeing some blank stares, but I have diagrams. Diagrams that very clearly represent what's going on when we do a git reset. So there are three options for reset. Soft, mixed, which is the default, and hard. Soft tends not to be used very frequently but this is just the first step of git reset.

[00:03:44]
Git reset --soft, and here I'm specifying HEAD~, which means the previous commit. All it does is move the head pointer. That's it. Now our head just points to the previous commit.
>> Nina Zakharenko: And that's our result, now-
>> Speaker 2: So is A still there, in case you wanna go back to it?

[00:04:07]
>> Nina Zakharenko: It's still there, but you can consider it dangling, you can consider it possibly a dangling commit at this point, because-
>> Speaker 2: So you'd have to move your head back there, in order for it to still-
>> Nina Zakharenko: Right.
>> Speaker 2: Okay.
>> Nina Zakharenko: Or make a branch off it, or recover it some other way.

[00:04:25]
If you make commits after you do a git reset soft, that A is gone. No more references to it, but.
>> Nina Zakharenko: The next step, git reset --mixed, the default. What it does, it moves the head,
>> Nina Zakharenko: And then, it copies the file from the commit that head is pointing to.

[00:04:51]
In this case, it's B, to the staging area.
>> Nina Zakharenko: Mixed is the default, you can also consider it the unstaged commands.
>> Nina Zakharenko: So after a git reset mixed, it looks like this.
>> Nina Zakharenko: Now, git reset hard, it does the same steps from soft and from mixed, but now it adds another one.

[00:05:17]
It does all three operations. First, it moves head. Next, it copies the file from commit B to our staging area. Lastly, it also copies it to the working area. This is a destructive operation. If your file in the working area had any changes, you just lost them. This operation can't be undone, so proceed with caution.

[00:05:45]
>> Nina Zakharenko: And this is the result of our git reset hard.
>> Nina Zakharenko: So a little bit of a cheat sheet here. If we git reset to a commit, we move the head and the current branch for soft. For mixed, we do that as well as reset the staging area.

[00:06:08]
For hard, we do both of those steps but in addition, we also reset the working area. Now, someone asked a question, what happens to commit B? Well git commit, git reset, excuse me, can change history. So our head was pointing at B.
>> Nina Zakharenko: Commit B had A as a parent.

[00:06:35]
Now, we reset soft, so we move the head. And sorry, that head should be pointing at A. My slides got a little messed up, but you guys get the gist. Head is pointing at A now. Now we make a new commit.
>> Nina Zakharenko: The new commit is pointing to A as a parent.

[00:06:57]
B has no references. And head has moved back up to that new commit that we made.
>> Nina Zakharenko: A stern warning, which you'll hear me say multiple times, never, ever push change history to a shared or public repository. You can really mess up other people's work.
>> Speaker 2: So what if something's been pushed, say, you pushed it and you're like, no that's not supposed to be there.

[00:07:29]
>> Nina Zakharenko: Yeah.
>> Speaker 2: Can you get it out once it's been pushed?
>> Nina Zakharenko: If I've pushed it to someplace like my public GitHub or remote repository where other people are working from, don't change it. You can change it locally, and I'll show you another strategy in just a few minutes for how to recover from that situation.

[00:07:49]
>> Nina Zakharenko: Finally, git reset file. What that does, doesn't move the head pointer but it does the same thing as a mixed reset. So it just copies that file from the staging area, from the commit to the staging area. If we do it from a commit, same thing. I can say git reset, commit C, put that file in my staging area.

[00:08:16]
So a little cheat sheet, if you git reset for a file, all it does is step two. It resets the staging area. You can't specify git reset commit in a file with any flags. It doesn't work with soft or hard, only mixed. If you wanna undo a git reset, this is a great strategy.

[00:08:37]
There is a reference called original head, or also called ORIG_HEAD. When you do an operation, like a reset or a merge, it saves where HEAD was before you did that operation. So if you wanna go back to the way things were, you can git reset to ORIG_HEAD,
>> Nina Zakharenko: And that'll get you right back to where you started.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now