git reset

Different types of git reset and when to use them

· by Aron Kovacs

1. what is git reset?

Git reset is a command that moves the HEAD and branch refs to a specified commit. It's commonly used to undo changes or move between different commit states.

2. types of reset

There are three main types of git reset:

Soft Reset: Moves HEAD to specified commit but keeps changes staged:

git reset --soft HEAD~1

Mixed Reset (Default): Moves HEAD and unstages changes:

git reset HEAD~1 git reset --mixed HEAD~1

Hard Reset: Moves HEAD and discards all changes (be careful!):

git reset --hard HEAD~1
3. common use cases

Reset to specific commit using hash:

git reset --hard abc123

Undo last commit but keep changes:

git reset --soft HEAD~1

Reset to remote branch state:

git reset --hard origin/main
4. finding commit hashes

View commit history with hashes:

git log

Compact view of commits:

git log --oneline

Search commits by message:

git log --grep="search term"
Note: Be very careful with --hard reset as it permanently discards changes. Make sure to commit or stash any important changes before using it. You can usually use just the first 6-8 characters of a commit hash.