Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there something like `git reset --hard --dry-run`?

Tags:

git

reset

dry-run

As I know that git reset --hard can potentially cause unwanted data loss because it may make changes that cannot be undone, is there a way to check what it would do before I execute it? Basically I'm looking for something that would be logically

git reset --hard --dry-run

or

git reset --hard --verbose --dry-run

which unfortunately do not work with currently existing Git because reset doesn't support --dry-run.

like image 647
Mikko Rantalainen Avatar asked Jan 27 '26 09:01

Mikko Rantalainen


2 Answers

I would dispute the premise of the question. I hard reset nimbly and freely all day long and I don't risk anything. If everything is nicely tucked into bed in a commit, hard reset is perfectly safe and undoable (especially if you make a placeholder branch first). So it's just a matter of making sure that everything is tucked in. git status will tell you that.

Also, consider git switch --det as an alternative. It does much the same thing as a hard reset but it has safety checks built in. You can always switch first and move the branch name later. Again, I do this a lot.

Finally, I should just add that I have an alias that lets me say git snapshot to make assurance double sure:

snapshot = !git stash push --include-untracked --message \"snapshot: $(date)\" && git stash apply \"stash@{0}\" --index

If you snapshot first, the next move is safe, because you can always get back to the commit you were on and apply the stash.

like image 159
matt Avatar answered Jan 30 '26 03:01

matt


git reset --hard doesn't have dry run mode but this should get you close to the same information:

#!/bin/bash
echo "Changes that would be executed to working directory files:"
git diff -R HEAD
echo "Staged changes that would be lost:"
git diff --cached --stat

or as a single-liner:

git diff -R HEAD && git diff --cached --stat
like image 39
Mikko Rantalainen Avatar answered Jan 30 '26 02:01

Mikko Rantalainen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!