Git reset - hard

How to use git reset --hard

Panjeh
2 min readJan 30, 2021

You are here since you need to undo/revert your changes.

Important note:

Be careful with the following commands, this throws away all your uncommitted changes and may delete files permanently!

Scenario 1:

You only have some modifications in your (tracked) file/files without staging them.

You can undo/revert all your changes with:

git reset --hard

Scenario 2:

You only have some modifications in your (tracked) file/files and also have staged them.

You can undo/revert all your changes with:

git reset --hard

Scenario 3:

You did some modifications in your (tracked) files and also created some new files without staging any of them.

You can undo/revert all your changes in your tracked files with:

git reset --hard

This command will only reset (undo/revert) all the changes inside the tracked files inside the main directory or any other directories.

This command does not touch any new (untracked) files inside the main or any other directories , since you have not staged them.

You need first to look at my other blog post (here) and then come back here.

Scenario 4:

You did some modifications in your (tracked) files, created some new files and also have staged all of them (new files and modified ones) by git add . ( dot here means you staged all the files)

This command will undo/revert all your changes in tracked files and also REMOVE all the untracked new files which are staged.

git reset --hard

It means for staged untracked new files when running the above command, the following command will also be run:

git clean -f -d

In conclusion:

All the modified tracked files regardless of whether they are staged or not, will be reverted with this command and only those staged untracked new files will be removed.

git reset --hard

To understand the above command you need to read this.

Learn More:

This also is equivalent:

git reset --hard HEAD

where HEAD is the last commit in your current branch

HEAD is simply a reference to the current commit (latest) on the current branch. There can only be a single HEAD at any given time (excluding git worktree).

The content of HEAD is stored inside .git/HEAD, and it contains the 40 bytes SHA-1 of the current commit.

I would like to introduce two packages for Laravel that I have recently developed: Laravel Pay Pocket, a modern multi-wallet package, and Laravel Failed Jobs, a UI for the Laravel Failed Jobs Table. I hope they may be of help to you.

https://github.com/HPWebdeveloper/laravel-pay-pocket
https://github.com/HPWebdeveloper/laravel-failed-jobs

--

--