Doubt: Difference between git reset --hard vs git clean

I am bit confused about how these two command works git reset --hard <commit id> and git clean -f

As per my understanding , git clean -f will remove untracked files from working directory and git reset --hard will remove all changes done after commit id passed

Please correct me If I’m wrong.

Thanks in advance.

Yes, it will delete them. If you pass -f you’re telling it to forcibly do it as well, which is a bit dangerous. Regardless of which option is used, if you have forgotten to commit file/s you meant to commit, the command will just fry them. There’s not going to be anything you can do about it.

It’s normally for doing something like wiping out build directories/files that shouldn’t be committed.

Not quite. --hard will remove any files that have appeared since, so in that respect, yes, it may exhibit similar behaviour. And it will nix all changes to existing files. Without --hard it will unstage all changes made after the commit, which is the command’s normal behaviour.

It is the opposite of git add

So if you’ve added some files to be committed that you didn’t mean to, you can run git reset to unstage them, then add the ones you actually wanted to add.

Note you’ll likely get into a mess if you try to go back and unstage commits that were done by other people, but it’s normally fine if it’s just a series of your commits.


Note that if you type

man git-clean

into the terminal that will give you the documentation for git clean, and if you type

man git-reset

it will give you the documentation for git reset

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.