Reset

It is a way to discard current changes. At least this is the most common use case. There are other uses for git reset but we will focus on this application only in the course.












Soft reset

Git soft reset will move the HEAD to a previous commit but will keep the changes in the working directory. This is useful when you want to keep the changes and maybe put them on a branch instead.

git reset --soft <commit-ish>

Example: Undo the last commit

git reset --soft HEAD~1

If we do a git status we will see that the changes that were reset are in the index (i.e. staged).












Changing a previous commit

We have two options here.

  1. Amend the commit

    git commit --amend
    

    This will open the editor with the previous commit message. We can change the message and save the file with :wq and then press Enter.

  2. Soft reset

    git reset --soft <commit-ish>
    

    This will move the HEAD to the previous commit but keep the changes in the working directory. We can then commit the changes again.

Remember

Both of these are changing history and creating new SHAs.












Hard reset

This is the most dangerous reset. It will move the HEAD to a previous commit and discard all changes in the working directory.

git reset --hard <commit-ish>

Example: Discard all changes

# Undo the last commit
git reset --hard HEAD~1
# Undo all changes and reset to latest commit on origin/main
git reset --hard origin/main











Exercise: Reset a commit

  • Navigate to a repository.
  • Create a new file reset.md.
  • Add some text to the file and commit it with message want to reset this.
  • Undo the commit by hard resetting it.
  • Check the logs.











Solution: Reset a commit

Navigate to a repository. Create a new change and commit it.

echo "reset" >> reset.md
git add reset.md
git commit -m "want to reset this"

Then we delete the change, which is the last commit, but hard resetting.

git reset --hard HEAD~1

Check the logs.

git log --oneline


Observation

Only changes that git knows about will be deleted in the hard reset. Any untracked changes will remain.

Warning

It is easy to loose changes that are staged but not committed.