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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With