Advertisement

Discard Local Changes

Discard changes to a specific file.

git checkout -- <filename>

Using restore command:

git restore <filename>

Discard all local changes:

git checkout .

Unstage Files

Remove files from staging area.

git reset HEAD <filename>

Using restore command:

git restore --staged <filename>

Unstage all files:

git reset HEAD

Reset Commits

Soft reset - keep changes in staging:

git reset --soft HEAD~1

Mixed reset - keep changes unstaged:

git reset HEAD~1

Hard reset - discard all changes:

git reset --hard HEAD~1

Reset to specific commit:

git reset --hard <commit-hash>

Revert Commits

Create new commit that undoes changes.

git revert <commit-hash>

Revert last commit:

git revert HEAD

Revert without creating commit:

git revert -n <commit-hash>

Revert multiple commits:

git revert <oldest-commit>..<newest-commit>

Amend Last Commit

Modify the most recent commit.

git commit --amend

Amend without changing message:

git commit --amend --no-edit
Advertisement

Clean Untracked Files

Remove untracked files from working directory.

git clean -f

Preview what will be deleted:

git clean -n

Remove untracked files and directories:

git clean -fd

Remove ignored files too:

git clean -fdx

Restore File from Specific Commit

Restore file to version from specific commit.

git checkout <commit-hash> -- <filename>

Using restore:

git restore --source=<commit-hash> <filename>

Undo Last Push

Reset local and force push to remote (dangerous).

git reset --hard HEAD~1
git push --force

Recover Deleted Commits

View reflog to find lost commits.

git reflog

Restore to a specific state:

git reset --hard <commit-hash>

Discard All Local Changes

Reset to match remote branch exactly.

git fetch origin
git reset --hard origin/<branch-name>

Undo Merge

Abort merge in progress.

git merge --abort

Reset to before merge:

git reset --hard ORIG_HEAD
Last updated: January 2026
Advertisement